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,7 +164,8 @@ func (qs *QuadStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bool
return newit, true return newit, true
case graph.All: case graph.All:
linkit := &SQLLinkIterator{ linkit := &SQLLinkIterator{
size: qs.Size(), tableName: newTableName(),
size: qs.Size(),
} }
for _, t := range primary.Tagger().Tags() { for _, t := range primary.Tagger().Tags() {
linkit.tagdirs = append(linkit.tagdirs, tagDir{ linkit.tagdirs = append(linkit.tagdirs, tagDir{

View file

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

View file

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

View file

@ -103,7 +103,7 @@ func (n *SQLNodeIntersection) buildSubqueries() []tableDef {
for i, it := range n.nodeIts { for i, it := range n.nodeIts {
var td tableDef var td tableDef
var table string 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.table = fmt.Sprintf("\n(%s)", table[:len(table)-1])
td.name = n.nodetables[i] td.name = n.nodetables[i]
out = append(out, td) out = append(out, td)
@ -159,11 +159,14 @@ func (n *SQLNodeIntersection) buildWhere() (string, []string) {
return query, vals 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() topData := n.tableID()
tags := []tagDir{topData} tags := []tagDir{topData}
tags = append(tags, n.getTags()...) tags = append(tags, n.getTags()...)
query := "SELECT DISTINCT " query := "SELECT "
if topLevel {
query += "DISTINCT "
}
var t []string var t []string
for _, v := range tags { for _, v := range tags {
t = append(t, v.String()) t = append(t, v.String())

View file

@ -160,11 +160,14 @@ func (n *SQLNodeIterator) buildWhere() (string, []string) {
return query, vals 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() topData := n.tableID()
tags := []tagDir{topData} tags := []tagDir{topData}
tags = append(tags, n.getTags()...) tags = append(tags, n.getTags()...)
query := "SELECT DISTINCT " query := "SELECT "
if topLevel {
query += "DISTINCT "
}
var t []string var t []string
for _, v := range tags { for _, v := range tags {
t = append(t, v.String()) t = append(t, v.String())