Speed up query with multiple conditions

时间:2019-05-20 13:41:31

标签: postgresql

I have a sql table of 5,000,000 entries with 5 columns over which I need to make a 4-condition (TEXT type) select. The table has columns id, name, street, city, zip and my select looks like this

SELECT id FROM register WHERE name=%s AND zip=%s AND city=%s AND street=%s

Problem is, i need to speed up this query, because i need to do 80 000 of this queries and now it takes half a day.

1 个答案:

答案 0 :(得分:0)

%s占位符表示您的所有四列都是varchar或text。如果是这样,那么以下索引可能会有所帮助:

CREATE INDEX idx ON register (name, zip, city, street, id)

索引的前四部分覆盖WHERE子句,第五部分覆盖id子句所需的SELECT列。