I have an accounts table with ~200k rows and an index for these columns:
account_type_id BIGINT
, member_id BIGINT
, external_id VARCHAR(64)
CREATE INDEX account_full_qualifiers_idx
ON normal_object.account (account_type_id, member_id, external_id) TABLESPACE index_tbsp;
And I have a function that is performing some ETL work with the following query:
EXECUTE '
SELECT * FROM normal_object.account
WHERE account_type_id = $1
AND member_id = $2
AND external_id = $3'
INTO e_row
USING r_row.account_type_id, r_row.member_id, r_row.external_id;
Yet, the EXECUTE command is NOT using the index and I'm not sure why. My only guess is that the data types are not lining up. Yet r_row.account_type_id is a BIGINT, r_row.member_id is a BIGINT, and r_row.external_id is a VARCHAR(64).
Any suggestions as to why it is not using the index?
How can I get it to use the index? (I've already tried setting enable_seqscan off.)
SET enable_seqscan off? What was the effect? Why is the data type or your ID columnexternal_idvarchar(64)? What's in it exactly? How long are the strings and most importantly what is theCOLLATIONyour index was created with? It needs to match the collation setting of your session, or the index cannot be used. Details here. As first, quick test: remove the condition onexternal_idin your query and try again, to verify the column is the culprit. – Erwin Brandstetter Nov 12 '14 at 02:33EXPLAINon theSELECT(using dummy vars for the params) it uses the index. But when it's ran in the function it doesn't. I have been doing all of this stuff via psql, though .. not using PgAdmin or some other 3rd party software (although not sure why that would matter since the backend generates the explain plan - not the client). – Joishi Bodio Nov 12 '14 at 15:02- what you get with\d normal_object.account` in psql. – Erwin Brandstetter Nov 12 '14 at 17:47