BlankRain
(护群神兽之点赞狂魔)
1
- data prepare .
The dataset I use is the movie data showed in dgraph tours.
wget "https://github.com/dgraph-io/tutorial/blob/master/resources/1million.rdf.gz?raw=true" -O 1million.rdf.gz -q
dgraph live -r 1million.rdf.gz
- run query:
I found if both of starring
, actor.film
, performance.film
showed in shortest
function as bellow:
query x{
path as shortest(from: 0x138b3, to: 0x4, depth:2 ,numpaths: 2) {
actor.film
starring
performance.film
}
entity(func: uid(path)) {
uid
}
}
the CPU rise from 27% to 316%
and with no response
It looks like a Multiplicative relation
.
Is there any way to make it faster?
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"github.com/dgraph-io/dgo/v210"
"github.com/dgraph-io/dgo/v210/protos/api"
"google.golang.org/grpc"
)
func main() {
// Connect to Dgraph
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
if err != nil {
log.Fatalf("Failed to connect: %v", err)
}
defer conn.Close()
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
// Note: Assumes dataset is pre-loaded via 'dgraph live'
fmt.Println("Assuming 1million.rdf.gz is pre-loaded.")
// Run optimized query
start := time.Now()
err = runQuery(dg)
if err != nil {
log.Fatalf("Query failed: %v", err)
}
fmt.Printf("Query completed in %v\n", time.Since(start))
}
func runQuery(dg *dgo.DgraphClient) error {
ctx := context.Background()
txn := dg.NewReadOnlyTxn()
defer txn.Discard()
// Optimized query: Single predicate, numpaths reduced
query := `
{
path as shortest(from: 0x138b3, to: 0x4, depth: 2, numpaths: 1) {
starring # Single predicate to reduce complexity
}
entity(func: uid(path)) {
uid
}
}`
resp, err := txn.Query(ctx, query)
if err != nil {
return fmt.Errorf("query error: %v", err)
}
// Parse and display result
var result map[string]interface{}
if err := json.Unmarshal(resp.Json, &result); err != nil {
return fmt.Errorf("unmarshal error: %v", err)
}
prettyJSON, _ := json.MarshalIndent(result, "", " ")
fmt.Println("Query result:")
fmt.Println(string(prettyJSON))
return nil
}