|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
do I need to create separate index in this case?
I have a table that has a composite primary key ( a primary key that has more than one column). For example, the primary key is on column_1 and column_2.
I realize that an index is created for these columns. My question is: If I am doing a select such as: "... WHERE column_2 = something" will the query benefit from the fact that the column_2 is part of the primary key or should I create a separate index that will speed up the performance of the query. Thanks. -gomer |
|
#2
|
||||
|
||||
|
Shouldn't make a difference...
keys are usually indexed for optimized searching composite primary keys are simply for keeping your table normalized and avoiding identical rows in your table. |
|
#3
|
|||
|
|||
|
Thanks MadCowDzz for your response.
I also received this answer to my question which I think is very useful: MySQL can't use a partial index if the columns don't form a leftmost prefix of the index. Suppose you have the SELECT statements shown here: mysql> SELECT * FROM tbl_name WHERE col1=val1; mysql> SELECT * FROM tbl_name WHERE col2=val2; mysql> SELECT * FROM tbl_name WHERE col2=val2 AND col3=val3; If an index exists on (col1, col2, col3), only the first of the preceding queries uses the index. The second and third queries do involve indexed columns, but (col2) and (col2, col3) are not leftmost prefixes of (col1, col2, col3). URL So in this case, declaring a separate column index is a good idea. |
![]() |
| Viewing: Dev Articles Community Forums > Databases > General SQL Development > do I need to create separate index in this case? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|