In MySQL, when I create a composite primary key, say with columns X, Y, Z, then all three columns become indexes automatically. Does the same happen for Postgres?
In MySQL, when I create a composite primary key, say with columns X, Y,
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you create a composite primary key, on
(x, y, z), PostgreSQL implements this with the help of oneUNIQUEmulti-column btree index on(x, y, z). In addition, all three columns areNOT NULL(implicitly), which is the main difference between aPRIMARY KEYand aUNIQUE INDEX.Besides obvious restrictions on your data, the multi-column index also has a somewhat different effect on the performance of queries than three individual indexes on
x,yandz.Related discussion on dba.SE:
With examples, benchmarks, discussion and outlook on the new feature of index-only scans in Postgres 9.2.
In particular, a primary key on
(x, y, z)will speed up queries with conditions onx,(x,y)or(x,y,z)optimally. It will also help with queries ony,z,(y,z)or(x,z)but to a far lesser extent.If you need to speed up queries on the latter combinations, you may want to change the order of column in your PK constraint and/or create one or more additional indexes. See: