MySQL - Unique index on multiple columns

WoLfPwNeR

The idea is to make fast queries based on columns c1, c2, and c3.

There are 3 kinds of queries:

SELECT COUNT(*) FROM tbl WHERE c1=... AND c2=...
SELECT c2, c3 FROM tbl WHERE c1=...
SELECT DISTINCT c2 FROM tbl WHERE c1=... AND c3=...

What is the best way to build index if I want to let (c1, c2) be unique?

I'm thinking of a single index like this:

UNIQUE INDEX idx_c1_c2 (c1, c2)

But the 3rd query wouldn't be using this index.

Any suggestions?

Rick James

WHERE c1= AND c2= needs INDEX(c1, c2) in either order. If that pair is "unique", then make it UNIQUE instead of INDEX. Or consider making it be the PRIMARY KEY (which is also UNIQUE and an INDEX).

WHERE c1= will happily use any kind of index starting with c1.

WHERE c1= AND c3= would use only the c1 part of (c1, c2), which would be somewhat useful. Better would be to add another index: INDEX(c1, c3) in either order. Suggest (c3, c1) to get some variety.

No single index will work "well" for all three queries.

Searching by PRIMARY KEY is usually faster than by UNIQUE INDEX if the table is Engine=InnoDB. If you have an AUTO_INCREMENT as the PRIMARY KEY now, consider whether it is worth keeping, versus replacing by the "natural" (c1, c2).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I specify unique constraint for multiple columns in MySQL?

From Java

pandas unique values multiple columns

From Java

How to set UNIQUE constraint to multiple columns MySQL?

From Dev

Index of three columns in mySQL

From Dev

How to allow NULL value for one column in unique index on multiple columns

From Dev

Insert into table or update if exists multiple unique index (MySQL)

From Dev

SQL multiple unique columns

From Dev

Conditional Unique Contraints On Multiple Columns

From Dev

Unique Values across multiple columns

From Dev

MySQL unique key not working as expected with multiple columns containing a primary key

From Dev

MYSQL Join tables with no unique id on multiple columns

From Dev

Best way to create Unique index in MYSQL on two columns when one column can contain empty values

From Dev

Mysql Unique index o = ö?

From Dev

Primary,Unique and Index in Mysql

From Dev

multiple unique key in a multi index

From Dev

Unique index on view with nullable columns

From Dev

SQL multiple unique columns

From Dev

Conditional Unique Contraints On Multiple Columns

From Dev

Summing columns based on unique values in multiple columns

From Dev

How to ignore unique Index on multiple columns based on column value while inserting new record

From Dev

mysql unique for multiple columns

From Dev

Index and match on multiple columns

From Dev

MYSQL Join tables with no unique id on multiple columns

From Dev

mysql unique index from set of columns

From Dev

Creating unique index over multiple columns that includes FK column

From Dev

How to fetch unique multiple columns?

From Dev

MySQL - A UNIQUE INDEX must include all columns in the table's partitioning function

From Dev

Update multiple columns in MySQL if unique ID exists

From Dev

MySQL Index on multiple columns

Related Related

  1. 1

    How do I specify unique constraint for multiple columns in MySQL?

  2. 2

    pandas unique values multiple columns

  3. 3

    How to set UNIQUE constraint to multiple columns MySQL?

  4. 4

    Index of three columns in mySQL

  5. 5

    How to allow NULL value for one column in unique index on multiple columns

  6. 6

    Insert into table or update if exists multiple unique index (MySQL)

  7. 7

    SQL multiple unique columns

  8. 8

    Conditional Unique Contraints On Multiple Columns

  9. 9

    Unique Values across multiple columns

  10. 10

    MySQL unique key not working as expected with multiple columns containing a primary key

  11. 11

    MYSQL Join tables with no unique id on multiple columns

  12. 12

    Best way to create Unique index in MYSQL on two columns when one column can contain empty values

  13. 13

    Mysql Unique index o = ö?

  14. 14

    Primary,Unique and Index in Mysql

  15. 15

    multiple unique key in a multi index

  16. 16

    Unique index on view with nullable columns

  17. 17

    SQL multiple unique columns

  18. 18

    Conditional Unique Contraints On Multiple Columns

  19. 19

    Summing columns based on unique values in multiple columns

  20. 20

    How to ignore unique Index on multiple columns based on column value while inserting new record

  21. 21

    mysql unique for multiple columns

  22. 22

    Index and match on multiple columns

  23. 23

    MYSQL Join tables with no unique id on multiple columns

  24. 24

    mysql unique index from set of columns

  25. 25

    Creating unique index over multiple columns that includes FK column

  26. 26

    How to fetch unique multiple columns?

  27. 27

    MySQL - A UNIQUE INDEX must include all columns in the table's partitioning function

  28. 28

    Update multiple columns in MySQL if unique ID exists

  29. 29

    MySQL Index on multiple columns

HotTag

Archive