Git Product home page Git Product logo

Comments (5)

nulian avatar nulian commented on June 11, 2024 1

yeah I am using the watch api.
Just added a quick simple index only on that field so the combined indexes probably would be better you came up with.

from spicedb.

josephschorr avatar josephschorr commented on June 11, 2024

@nulian Be curious to see an EXPLAIN output from your database, just so we can compare to ours

from spicedb.

nulian avatar nulian commented on June 11, 2024

Not precisly the same database(already put an index on it myself in that database) but is based on a dump from it but this is the explain

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE relation_tuple ALL ix_relation_tuple_by_deleted_transaction 224739 Using where

Without index it takes somewhere between 500ms and 5s
With index it's 10ms - 150ms

Also our slow query log was empty after putting the index on that column

from spicedb.

vroldanbet avatar vroldanbet commented on June 11, 2024

@nulian are you using the Watch API? Would you mind sharing the index you added?

This access pattern seems to be used by the MySQL datastore Watch API implementation:

sql, args, err := mds.QueryChangedQuery.Where(sq.Or{
sq.And{
sq.Gt{colCreatedTxn: afterRevision},
sq.LtOrEq{colCreatedTxn: newRevision},
},
sq.And{
sq.Gt{colDeletedTxn: afterRevision},
sq.LtOrEq{colDeletedTxn: newRevision},
},
}).ToSql()

this is the explain I got initially with 84K rows:

EXPLAIN
-> Filter: (((relation_tuple.created_transaction > 158582) and (relation_tuple.created_transaction <= 158583)) or ((relation_tuple.deleted_transaction > 158582) and (relation_tuple.deleted_transaction <= 158583))) (cost=7920 rows=16334) (actual time=71.3..71.3 rows=0 loops=1)
-> Table scan on relation_tuple  (cost=7920 rows=77839) (actual time=0.0697..58.1 rows=84849 loops=1)

|

Then I added the following index, the rows scanned where lower:

CREATE INDEX ix_watch
    ON relation_tuple (created_transaction, deleted_transaction DESC);

OR

CREATE INDEX ix_watch
    ON relation_tuple (created_transaction DESC, deleted_transaction DESC);
EXPLAIN
-> Filter: (((relation_tuple.created_transaction > 158582) and (relation_tuple.created_transaction <= 158583)) or ((relation_tuple.deleted_transaction > 158582) and (relation_tuple.deleted_transaction <= 158583))) (cost=0.804 rows=1) (actual time=0.0921..0.0921 rows=0 loops=1)
-> Sort-deduplicate by row ID  (cost=0.804 rows=1) (actual time=0.0902..0.0902 rows=0 loops=1)
    -> Index range scan on relation_tuple using ix_watch over (158582 < created_transaction <= 158583)  (cost=1.11 rows=1) (actual time=0.0387..0.0387 rows=0 loops=1)
    -> Index range scan on relation_tuple using ix_relation_tuple_by_deleted_transaction over (158582 < deleted_transaction <= 158583)  (cost=0.36 rows=1) (actual time=0.0342..0.0342 rows=0 loops=1)

|

EXPLAIN
-> Filter: (((relation_tuple.created_transaction > 158582) and (relation_tuple.created_transaction <= 158583)) or ((relation_tuple.deleted_transaction > 158582) and (relation_tuple.deleted_transaction <= 158583))) (cost=0.804 rows=1) (actual time=0.104..0.104 rows=0 loops=1)
-> Sort-deduplicate by row ID  (cost=0.804 rows=1) (actual time=0.102..0.102 rows=0 loops=1)
    -> Index range scan on relation_tuple using ix_watch over (158583 <= created_transaction < 158582)  (cost=1.11 rows=1) (actual time=0.0759..0.0759 rows=0 loops=1)
    -> Index range scan on relation_tuple using ix_relation_tuple_by_deleted_transaction over (158582 < deleted_transaction <= 158583)  (cost=0.36 rows=1) (actual time=0.00633..0.00633 rows=0 loops=1)

|

from spicedb.

vroldanbet avatar vroldanbet commented on June 11, 2024

@nulian perfect then I'll open a PR with the new proposed index

from spicedb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.