Обсуждение: [GENERAL] Different query plan used for the same query depending on howparameters are passed
[GENERAL] Different query plan used for the same query depending on howparameters are passed
This e-mail together with any attachments (the "Message") is confidential and may contain privileged information. If you are not the intended recipient or if you have received this e-mail in error, please notify the sender immediately and permanently delete this Message from your system. Do not copy, disclose or distribute the information contained in this Message.
Maven Investment Partners Ltd (No. 07511928), Maven Derivatives Ltd (No. 07511840) , MVN Asset Management Limited (No. 09659116), Maven Europe Ltd (No. 08966593), Maven Derivatives Asia Limited (No.10361312) & Maven Securities Holding Ltd (No. 07505438) are registered as companies in England and Wales and their registered address is Level 3, 6 Bevis Marks, London EC3A 7BA, United Kingdom. The companies’ VAT No. is 135539016. Maven Asia (Hong Kong) Ltd (No. 2444041) is registered in Hong Kong and its registered address is 20/F, 198 Wellington St, Hong Kong. Only Maven Derivatives Ltd and MVN Asset Management Limited are authorised and regulated by the Financial Conduct Authority (Maven Derivatives Ltd FRN: 607267, MVN Asset Management Limited FRN: 714429)
On 05/16/2017 06:01 AM, David Chapman wrote: > I have a table that includes two text columns t1 and t2, and a composite > index on these columns. When issuing a query of the following form: > > SELECT * FROM test WHERE t1 = 'X' and t2 = ANY(ARRAY['Y1', 'Y2', ..]) > > I have observed that it will use the index and have reasonable > performance if the whole query is passed as a single big string. However > if it is parameterised (I'm using Npgsql) it switches to doing a > sequence scan and performs terribly. What Postgres version? Can you show the parametrized version? What is the output if you add EXPLAIN ANALYZE to the beginning of the query?: https://www.postgresql.org/docs/9.6/static/sql-explain.html > > The table contains approx 2.3 million records and the query matches > about 20k records (i.e. there are 20k 'Y' values in the array). > > I have experimented with changing work_mem, preparing the statement in > advance, ANALYZEing the table, none of these change the behavior. > > Why does the query planner choose to ignore the index when the command > is parameterised? > > > -- Adrian Klaver adrian.klaver@aklaver.com
From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of David Chapman
Sent: Tuesday, May 16, 2017 9:02 AM
To: pgsql-general@postgresql.org
Subject: [GENERAL] Different query plan used for the same query depending on how parameters are passed
Attention: This email was sent from someone outside of Perceptron. Always exercise caution when opening attachments or clicking links from unknown senders or when receiving unexpected emails.
I have a table that includes two text columns t1 and t2, and a composite index on these columns. When issuing a query of the following form:
SELECT * FROM test WHERE t1 = 'X' and t2 = ANY(ARRAY['Y1', 'Y2', ..])
I have observed that it will use the index and have reasonable performance if the whole query is passed as a single big string. However if it is parameterised (I'm using Npgsql) it switches to doing a sequence scan and performs terribly.
The table contains approx 2.3 million records and the query matches about 20k records (i.e. there are 20k 'Y' values in the array).
I have experimented with changing work_mem, preparing the statement in advance, ANALYZEing the table, none of these change the behavior.
Why does the query planner choose to ignore the index when the command is parameterised?
It’s because when optimizer builds execution plan for parametrized queiry, it doesn’t know what values for t1 and t2 will be provided for WHERE clause.
Regards,
Igor Neyman
On 05/16/2017 06:01 AM, David Chapman wrote:I have a table that includes two text columns t1 and t2, and a composite index on these columns. When issuing a query of the following form:
SELECT * FROM test WHERE t1 = 'X' and t2 = ANY(ARRAY['Y1', 'Y2', ..])
I have observed that it will use the index and have reasonable performance if the whole query is passed as a single big string. However if it is parameterised (I'm using Npgsql) it switches to doing a sequence scan and performs terribly.
What Postgres version?
Can you show the parametrized version?
What is the output if you add EXPLAIN ANALYZE to the beginning of the query?:
https://www.postgresql.org/docs/9.6/static/sql-explain.html --
The table contains approx 2.3 million records and the query matches about 20k records (i.e. there are 20k 'Y' values in the array).
I have experimented with changing work_mem, preparing the statement in advance, ANALYZEing the table, none of these change the behavior.
Why does the query planner choose to ignore the index when the command is parameterised?
Adrian Klaver
adrian.klaver@aklaver.com
This e-mail together with any attachments (the "Message") is confidential and may contain privileged information. If you are not the intended recipient or if you have received this e-mail in error, please notify the sender immediately and permanently delete this Message from your system. Do not copy, disclose or distribute the information contained in this Message.
Maven Investment Partners Ltd (No. 07511928), Maven Derivatives Ltd (No. 07511840) , MVN Asset Management Limited (No. 09659116), Maven Europe Ltd (No. 08966593), Maven Derivatives Asia Limited (No.10361312) & Maven Securities Holding Ltd (No. 07505438) are registered as companies in England and Wales and their registered address is Level 3, 6 Bevis Marks, London EC3A 7BA, United Kingdom. The companies’ VAT No. is 135539016. Maven Asia (Hong Kong) Ltd (No. 2444041) is registered in Hong Kong and its registered address is 20/F, 198 Wellington St, Hong Kong. Only Maven Derivatives Ltd and MVN Asset Management Limited are authorised and regulated by the Financial Conduct Authority (Maven Derivatives Ltd FRN: 607267, MVN Asset Management Limited FRN: 714429)
David Chapman <david.chapman@mavensecurities.com> writes: > Here is the output of EXPLAIN ANALYZE on the two queries. > Index Scan using test_index_t1_t2 on test (cost=0.43..684.11 rows=71 > width=245) (actual time=0.022..1.147 rows=99 loops=1) > Index Cond: ((t1 = 'X'::bpchar) AND (t2 = ANY ('{2286575,2139022,2139030, ^^^^^^^^^^^^^^^^^^ > Seq Scan on test (cost=0.00..403725.30 rows=1 width=245) (actual > time=47.543..5362.518 rows=99 loops=1) > Filter: (((t1)::text = 'X'::text) AND (t2 = ANY ^^^^^^^^^^^^^^^^^^^^^^^^ There's your problem. t1 is evidently of char(n) type, and when you write "t1 = 'X'" the literal also becomes char(n) and so you have a condition that can match the index. But the parameter is evidently being assigned type text, which wins the type conflict so then you have "t1::text text-eq text-constant", and that operator doesn't match the index. Probably, casting the parameter to char(n) explicitly would fix this. regards, tom lane