![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| ||||||||
| ||||||||
|
|
I've found myself in the curious situation where it makes sense for me to cover an entire database table with an index, like so: |
|
create table test(id integer primary key, first integer, second integer, third integer); create index idx on test(id, first, second, third); |
|
Admittedly, I'm currently using sqlite rather than mysql but this is just a stop-gap solution and I'm planning to migrate to mysql in the future. However, I'm sure they will have similar issues... The database with the covering index is more than twice the size as one without it. |
|
The size, itself, is not necessarily a concern but I've heard that this can hurt performance, even to the point of being more efficient without the covering index. |
|
I was hoping that sqlite would be clever enough to do without the original table and only use the index but it appears not to be. Would mysql handle this better? |
|
Would this covering index be worth it? |
|
Would be better to compromise and just cover half the table? |
|
More generally, when are covering indeces most effective? |
#3
| ||||
| ||||
|
|
create table test(id integer primary key, first integer, second integer, third integer); create index idx on test(id, first, second, third); |
|
The database with the covering index is more than twice the size as one without it. |
|
Would mysql handle this better? |
|
Would this covering index be worth it? Would be better to compromise and just cover half the table? |
#4
| |||
| |||
|
|
I've found myself in the curious situation where it makes sense for me to cover an entire database table with an index, like so: create table test(id integer primary key, first integer, second integer, third integer); create index idx on test(id, first, second, third); That makes no sense under any circumstances. |
#5
| |||||
| |||||
|
|
In article<NH%Bq.76784$ql3.52770 (AT) en-nntp-12 (DOT) dc1.easynews.com>, Just Another Victim of the Ambient Morality<spam_receptacle_ (AT) hotmail (DOT) com> wrote: I've found myself in the curious situation where it makes sense for me to cover an entire database table with an index, like so: Could you explain how you came to this conclusion? What do you see as the purpose for having such an index? What problem does it solve? |
|
create table test(id integer primary key, first integer, second integer, third integer); create index idx on test(id, first, second, third); |
|
Since id is unique, adding first, second and third doesn't provide any further qualification; it just makes those values available directly from the index instead. However, it also makes the index entries larger, so potentially fewer of them could be cached. |
|
Admittedly, I'm currently using sqlite rather than mysql but this is just a stop-gap solution and I'm planning to migrate to mysql in the future. However, I'm sure they will have similar issues... The database with the covering index is more than twice the size as one without it. It would be, since you are storing every data item twice: once in the table row and again in the index. And the index will also have structure information for efficient lookup (e.g. a tree). |
|
They only help for queries that (a) can use the index to locate the required data, AND (b) only want to fetch columns that are in the index, so that they don't have to also fetch the row pointed to by the index entry. |
#6
| |||
| |||
|
|
On 12/2/2011 3:08 AM, Just Another Victim of the Ambient Morality wrote: I've found myself in the curious situation where it makes sense for me to cover an entire database table with an index, like so: create table test(id integer primary key, first integer, second integer, third integer); create index idx on test(id, first, second, third); That makes no sense under any circumstances. Since id is the primary key, it is by definition unique -- and therefore, so is the combination of (id, first, second, third). So make that (id, first, second, third) the primary key instead of id alone, and get rid of that silly index. |
#7
| |||
| |||
|
|
I don't know SQL well enough to understand how you would make a _primary_ key of several columns. How does that work? |
#8
| |||
| |||
|
|
On 12/2/2011 4:52 PM, Just Another Victim of the Ambient Morality wrote: I don't know SQL well enough to understand how you would make a _primary_ key of several columns. How does that work? It works the same way as creating a primary key with only one column. Have you looked at the documentation yet? |
#9
| |||
| |||
|
|
On 12/2/2011 3:08 AM, Just Another Victim of the Ambient Morality wrote: I've found myself in the curious situation where it makes sense for me to cover an entire database table with an index, like so: create table test(id integer primary key, first integer, second integer, third integer); create index idx on test(id, first, second, third); That makes no sense under any circumstances. Since id is the primary key, it is by definition unique -- and therefore, so is the combination of (id, first, second, third). So make that (id, first, second, third) the primary key instead of id alone, and get rid of that silly index. |
#10
| |||
| |||
|
|
On 12/2/2011 3:37 PM, Doug Miller wrote: On 12/2/2011 3:08 AM, Just Another Victim of the Ambient Morality wrote: I've found myself in the curious situation where it makes sense for me to cover an entire database table with an index, like so: create table test(id integer primary key, first integer, second integer, third integer); create index idx on test(id, first, second, third); That makes no sense under any circumstances. Since id is the primary key, it is by definition unique -- and therefore, so is the combination of (id, first, second, third). So make that (id, first, second, third) the primary key instead of id alone, and get rid of that silly index. Since id is already unique, all this will do is needlessly waste space, buffer, etc. Terrible idea. He wants an index on all those columns, that's how he can get one. |
![]() |
| Thread Tools | |
| Display Modes | |
| |