I just checked the design of some core tables and to my surprise I think that there are a lot of redundant indexes on core drupal tables.
Removing those indexes could result in a huge performance benefit while inserting or updating (not to mention a reduced database size).
Let us examine for example the table term_node:
CREATE TABLE `term_node` (
`nid` int(10) unsigned NOT NULL default '0',
`tid` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`tid`,`nid`),
KEY `nid` (`nid`),
KEY `tid` (`tid`)
)
There are three indexes on this table:
primary key, columns: tid, nid (important is the ordering of the columns in the primary key)
index nid, column: nid
index tid, column: tid
The index tid is a non necessary index. Queries that would never use it since the first column on the primary key is the same.
Also on the node!!! table we same:
primary key, columns: ,nid,vid
index nid, column: nid
Testcase on term_node table:
- remove the index tid
-
explain extended select * from term_node where tid = 6;
As far as I can see, the query is using the primary key index.
I used to be a Oracle developer and the above is definately true for Oracle database. I am not for 100% sure if it is also true for Mysql but the explain query shows that they are redundant.
Please let me know if I am wrong here.
-----------------------------------------
Joep
CompuBase, Drupal websites and design
Comments
Comment #1
kbahey commentedThe other side of the argument is this article: Redundant index is not always bad.
Of course, depends on data set size and cardinality, so hard to come with a blanket rule.
Can you (or anyone else) run benchmarks with/without the redundant indexes and see if there is a noticable difference?
Comment #2
Anonymous (not verified) commentedI changed the version to 6.x-dev.
However the origal issue was based on D5.
The same issue is still on D6.
For example the table term_node:
The index vid is redundant since it is the first column of the primary key.
Comment #3
Anonymous (not verified) commented@kbahey
The article you mentioned shows exactly where the pain is:
I assume that the default design of a table is to not use redundant indexes.
There could be situation where you could benefit where the index has all the columns you are looking for.
But that is not the case here.
I have not checked al tables but I assume that the standard Drupal table design is to create an index for every primary key column.
Cheers, Joep
Comment #4
catchThis should be merged with:
http://drupal.org/node/164532
Comment #5
Anonymous (not verified) commentedI agree that this should be merged. Thanks for pointing to the other issue.
I am not sure whethe the status should be set to duplicate since the issues are related but are not discussing the same problem.
Comment #6
peterx commentedPerhaps we could set up a common page describing the reasons for indexing certain ways and reference the page in both issues. A person can then find either issue from a search and reach the description page.
Comment #7
catchhttp://drupal.org/files/issues/schemas.patch
This patch I believe removes the redundant index, going to re-roll it for http://drupal.org/node/164532 so these two will be properly merged. Setting this back to active in case that doesn't get in - so this can be dealt with in it's own right later.
Comment #8
Anonymous (not verified) commentedThanks, but I think the patch is not complete. For, example the node table is not listed.
Comment #9
dpearcefl commentedIs this still a problem in current D6?
Comment #10
dpearcefl commentedComment #11
catchIndexes were overhauled in D7.