Fix up hash interface and speed up save queries 10x

Fix all optimizer
This commit is contained in:
Barak Michener 2015-08-10 15:41:22 -04:00
parent ab3f59d21f
commit aedd0401e2
5 changed files with 26 additions and 12 deletions

View file

@ -164,6 +164,7 @@ func (qs *QuadStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bool
return newit, true
case graph.All:
linkit := &SQLLinkIterator{
tableName: newTableName(),
size: qs.Size(),
}
for _, t := range primary.Tagger().Tags() {

View file

@ -290,7 +290,7 @@ func (it *SQLIterator) makeCursor(next bool, value graph.Value) error {
}
var q string
var values []string
q, values = it.sql.buildSQL(next, value, false)
q, values = it.sql.buildSQL(next, value, true)
q = convertToPostgres(q, values)
ivalues := make([]interface{}, 0, len(values))
for _, v := range values {

View file

@ -71,7 +71,7 @@ type sqlItDir struct {
type sqlIterator interface {
sqlClone() sqlIterator
buildSQL(next bool, val graph.Value, hash bool) (string, []string)
buildSQL(next bool, val graph.Value, topLevel bool) (string, []string)
getTables() []tableDef
getTags() []tagDir
buildWhere() (string, []string)
@ -246,10 +246,13 @@ func (l *SQLLinkIterator) tableID() tagDir {
}
}
func (l *SQLLinkIterator) buildSQL(next bool, val graph.Value, hash bool) (string, []string) {
query := "SELECT DISTINCT "
func (l *SQLLinkIterator) buildSQL(next bool, val graph.Value, topLevel bool) (string, []string) {
query := "SELECT "
if topLevel {
query += "DISTINCT "
}
hashs := ""
if hash {
if !topLevel {
hashs = "_hash"
}
t := []string{
@ -270,14 +273,18 @@ func (l *SQLLinkIterator) buildSQL(next bool, val graph.Value, hash bool) (strin
t = append(t, fmt.Sprintf("%s as %s", k.table, k.name))
}
query += strings.Join(t, ", ")
query += " WHERE "
constraint, wherevalues := l.buildWhere()
if constraint != "" {
query += " WHERE "
}
values = append(values, wherevalues...)
if !next {
v := val.(quad.Quad)
if constraint != "" {
constraint += " AND "
} else {
constraint += " WHERE "
}
t = []string{
fmt.Sprintf("%s.subject_hash = ?", l.tableName),

View file

@ -103,7 +103,7 @@ func (n *SQLNodeIntersection) buildSubqueries() []tableDef {
for i, it := range n.nodeIts {
var td tableDef
var table string
table, td.values = it.buildSQL(true, nil, true)
table, td.values = it.buildSQL(true, nil, false)
td.table = fmt.Sprintf("\n(%s)", table[:len(table)-1])
td.name = n.nodetables[i]
out = append(out, td)
@ -159,11 +159,14 @@ func (n *SQLNodeIntersection) buildWhere() (string, []string) {
return query, vals
}
func (n *SQLNodeIntersection) buildSQL(next bool, val graph.Value, _ bool) (string, []string) {
func (n *SQLNodeIntersection) buildSQL(next bool, val graph.Value, topLevel bool) (string, []string) {
topData := n.tableID()
tags := []tagDir{topData}
tags = append(tags, n.getTags()...)
query := "SELECT DISTINCT "
query := "SELECT "
if topLevel {
query += "DISTINCT "
}
var t []string
for _, v := range tags {
t = append(t, v.String())

View file

@ -160,11 +160,14 @@ func (n *SQLNodeIterator) buildWhere() (string, []string) {
return query, vals
}
func (n *SQLNodeIterator) buildSQL(next bool, val graph.Value, _ bool) (string, []string) {
func (n *SQLNodeIterator) buildSQL(next bool, val graph.Value, topLevel bool) (string, []string) {
topData := n.tableID()
tags := []tagDir{topData}
tags = append(tags, n.getTags()...)
query := "SELECT DISTINCT "
query := "SELECT "
if topLevel {
query += "DISTINCT "
}
var t []string
for _, v := range tags {
t = append(t, v.String())