Repository navigation
Description
Describe the bug
Index is not used in the WHERE clause.
How are you accessing AGE (Command line, driver, etc.)?
- JDBC
What data setup do we need to do?
LOAD 'age';
SET search_path = ag_catalog, "$user", public;
select ag_catalog.create_graph('test_graph');
select ag_catalog.create_vlabel('test_graph','profile');
-- works only for MATCH because GIN only makes sense for pattern matching what MATCH clause is
CREATE INDEX profile_gin_idx ON test_graph."profile" USING GIN (properties);
-- does not work in the where clause
CREATE INDEX profile_pet_btree_idx1 ON test_graph."profile" USING BTREE ((properties -> 'pet'));
-- does not work in the where clause
CREATE INDEX profile_pet_btree_idx2 ON test_graph."profile" USING BTREE (ag_catalog.agtype_access_operator(properties, '"pet"'::ag_catalog.agtype));
------ generate more data -------
DO
$do$
BEGIN
FOR i IN 1..10000 LOOP
EXECUTE format('
select * from ag_catalog.cypher(''test_graph'',
$$
CREATE (any_vertex: profile { `id`: "%s", `pet`: "%s", `hidden`: %s })
RETURN any_vertex
$$
) as (any_vertex ag_catalog.agtype)',
(SELECT uuid_in(md5(random()::text || now()::text)::cstring)),
(SELECT ('[0:2]={dog,cat,bird}'::text[])[floor(random()*3)]),
(SELECT ('[0:1]={true,false}'::text[])[floor(random()*2)])
);
END LOOP;
END
$do$;
What is the command that caused the error?
select
any_profile
from ag_catalog.cypher('test_graph',$$
EXPLAIN ANALYZE MATCH (any_profile:`profile` { hidden: false })
WHERE any_profile.pet = 'dog'
RETURN any_profile
$$
) as (any_profile ag_catalog.agtype);
| QUERY PLAN |
|---|
| Bitmap Heap Scan on profile any_profile (cost=20.08..52.40 rows=1 width=32) (actual time=1.111..22.752 rows=1616 loops=1) |
| Recheck Cond: (properties @> agtype_build_map('hidden'::text, 'false'::agtype)) |
| Filter: (agtype_access_operator(VARIADIC ARRAY[_agtype_build_vertex(id, _label_name('17463'::oid, id), properties), '"pet"'::agtype]) = '"dog"'::agtype) |
| Rows Removed by Filter: 3384 |
| Heap Blocks: exact=143 |
| -> Bitmap Index Scan on profile_gin_idx (cost=0.00..20.08 rows=10 width=0) (actual time=1.010..1.010 rows=5000 loops=1) |
| Index Cond: (properties @> agtype_build_map('hidden'::text, 'false'::agtype)) |
| Planning Time: 0.733 ms |
| Execution Time: 22.869 ms |
Expected behavior
Either of two indexes should be used
Environment (please complete the following information):
- Version: [e.g. 1.3.0]
Additional context
As we see from the plan the filter function is applied as:
Filter: (agtype_access_operator(VARIADIC ARRAY[_agtype_build_vertex(id, _label_name('17463'::oid, id), properties), '"pet"'::agtype]) = '"dog"'::agtype)
We also tried to simulate the functional index like this with no luck because there is a mutable function involved: \
CREATE INDEX profile_pet_btree_idx3 ON test_graph."profile" USING BTREE (
ag_catalog.agtype_access_operator(VARIADIC ARRAY[age_properties(_agtype_build_vertex(id, _label_name('16945'::oid, id), properties)), '"pet"'::ag_catalog.agtype])
);Could it be possible to add an index support for the WHERE clause at least via any functional index by not involving mutable functions ?
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
jrgemignani commentedon Jun 20, 2023
There is a patch in the works that "may" address this. But, others are welcome to look into it.
vladiksun commentedon Jun 21, 2023
@jrgemignani Is there a branch for the patch you mentioned ?
jrgemignani commentedon Jun 22, 2023
@vladiksun The patch I'm referring to isn't in a branch atm. Currently, it is under review for completeness.
jrgemignani commentedon Jun 27, 2023
@MuhammadTahaNaveed @Zainab-Saad Could you look into whether the patch (Josh's) you are reviewing/reworking might address this or not?
MuhammadTahaNaveed commentedon Jun 28, 2023
We checked and it doesnt seem that patch solves this issue.
jrgemignani commentedon Aug 8, 2023
@vladiksun We are still looking into this, it's been a busy past month.
jrgemignani commentedon Aug 8, 2023
edited: removed. I realized it wasn't what I thought it was. The index, apparently isn't used until there is a threshold reached.
jrgemignani commentedon Aug 10, 2023
@vladiksun I was able to get an index working for WHERE -
I still need to do other tests to verify that the changes don't cause any side effects. There are 2 issues here -
profile_pet_btree_idx3was not correct. But, without being able to create it, you wouldn't know.vladiksun commentedon Aug 10, 2023
@jrgemignani thank you for taking a look. Indeed without creating a functional index there is no way to check if that actuality might work.
If you were able to create such an index, one thing seems suspicious to me. With an index the actual execution time is longer than without it. But this probably depends on the setup.
For basic queries like searching the vertex by its property we decided not to use Cypher calls, but instead usual SQL queries with functional indexes around properties values. It is much faster and we can use native FTS this way. I believe this is possible because agtype is a superset of a jsonb hence we can use it like jsonb.
But for traversal queries the index should help reduce the filtering time of incoming or outgoing vertices.
For example queries with scenarios - find all outgoing vertices with label 'A' from the start vertex label 'B' where some 'A''s property IN [value1, value2]. I assume with more hops involved ('A's could be chained in a hierarchy) such queries take longer without index on a specific property.
jrgemignani commentedon Aug 10, 2023
@vladiksun There are 2 things that will cause a difference in execution time. The first is that I modified your for loop to 100000. The second is that I ran it locally on a small server of mine. Tomorrow I will try to give you before and after times with your original query.
jrgemignani commentedon Aug 10, 2023
@vladiksun Here are the results using your original query.
Before adding 3rd index
Adding 3rd index
After adding 3rd index
vladiksun commentedon Aug 10, 2023
@jrgemignani this looks promising, thank you. By the way can you get rid of using label id while creating the index definition? As I recall we've got this whole function call from the execution plan as it is. I assume this might involve changing the main logic so the code doesn't use label ID either. But when used with liquebase in the kubernetes environment this means we should precalculate the label id before we use it which is not convinient for schema initialization scripts. Ideally the index creation scripts should use predefined data if possible.
jrgemignani commentedon Aug 10, 2023
@vladiksun In the following query -
The cypher component -
any_profile is a vertex
In your first index below, I'm not sure if this has been set up. We will need to debug this specific index to find out how it is resolved.
In your second index below,
agtype_access_operatoraka.understands how to work with whatever value is sent to it. Theproperties()function isn't used asagtype_access_operatorhas to be able to accept any object or array. So, any.operation just takes the variable used.That is why this index worked -
I should note, this is after adjusting the volatility flag for the functions used in the index.
Our team needs to look into changing the volatility flag for the functions used in this index and similar ones for edges. Part of the problem originally was that some wanted to blanketly change that flag without bothering to truly understand if it can be changed. Changing the flag obviously works but does it pose other issues?
For you, as a temporary fix, all you need to do is change the volatility flag of the functions involved in
age--1.3.0.sqland then rebuild the extension. Or, possibly, alter their entries through sql.We will be looking into the issue with the volatility flag to see if it can be updated with a patch, sometime this week.
Hopefully, this is helpful.
22 remaining items
vladiksun commentedon Sep 8, 2023
@rafsun42 thank you. We will check this next week and let you know.
vladiksun commentedon Sep 11, 2023
@rafsun42 we checked. It works on my machine. This could be a good solution
jrgemignani commentedon Sep 16, 2023
@vladiksun I have another PR to enhance performance and indexes #1232
Also, I want to point out that, for whatever reason, your particular dataset and query does best without using a GIN index. I've checked our GIN index components and they are largely the same as PG's JSONB GIN components, so I'm thinking it's your dataset that just doesn't work well with a GIN index. I did find that a BTREE index, by itself, works best for your dataset. Not a BitmapAnd of the GIN and BTREE, which fell between GIN and BTREE performance gains.
On my server, the above PR cut the query time, for your queries above, in half without using any indexes. Adding the GIN index only gave a very minor improvement. The BTREE index (*_idx2) cut the query time in half again.
vladiksun commentedon Sep 18, 2023
@jrgemignani Thanks for sharing the PR. I have built that branch to see how it could affect our typical queries.
It turns out that for our use cases, we do not see much difference.
Usually, our typical queries are like this:
As you can see we use only one table (e_label) for edges and structures like
improve the execution time because there is a call to a GIN index that cuts unnecessary edges. That also helps simplify backend logic.
So, such a query:
works even faster than the version with lots of edge tables.
jrgemignani commentedon Sep 18, 2023
@vladiksun We should probably look into these later queries separately. My tests were only against your query at the top of this thread and only on my server. If the issue with using WHERE (unable to create an index or index isn't working) is resolved? we should probably close this thread and create one for performance improvements and link this thread.
Thoughts?
vladiksun commentedon Sep 18, 2023
@jrgemignani I believe @rafsun42 also suggested the solution for IN operator. I can move the issue with "IN" operator to another ticket. Let me know if it is convenient to do so or leave this issue open until the solution for "IN" operator is implemented.
jrgemignani commentedon Sep 18, 2023
@vladiksun We probably should keep the issues separate but link them. Really long threads can lose their readability, especially if they diverge too much. Also, having a long thread with many issues gives the impression that it is just one issue that is being worked on, when it can be many more.
vladiksun commentedon Sep 18, 2023
@jrgemignani, @rafsun42 ok I will create a new issue for the proposed solution for IN operator and link it to the this one. Thank you.
jrgemignani commentedon Sep 18, 2023
@vladiksun Yes, @rafsun42 and @dehowef have both been working on PRs for improving WHERE performance. Hopefully, we'll have them all out by the end of the week.
Fix MERGE to use indexes by removing volatile wrapper hack
Fix MERGE to use indexes by removing volatile wrapper hack