0

I have the following table (tracking searches):

CREATE TABLE `searches` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `deleted` tinyint(1) unsigned NOT NULL,
  `query` varchar(1024) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `userId` int(10) unsigned NOT NULL,
  `connectionId` int(10) unsigned NOT NULL,
  `pluginId` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `key,deleted` (`publicId`,`deleted`),
  KEY `deleted,userId` (`deleted`,`userId`),
  KEY `deleted,connectionId,pluginId` (`deleted`,`connectionId`,`pluginId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Sometimes, I run queries to return all the queries done by a specific user. In that case, I have an index with the following columns: deleted,userId

Other times, I run queries to return all the queries done by specific "connection" and "plugin". In that case, I have an index with the following columns: deleted,connectionId,pluginId

The size of the indexes are growing substantially, and so I'm trying to understand how I could better optimize these.

Would there be value in combine both indexes into one? For example, it would have the following columns: deleted,userId,connectionId,pluginId

I'm not sure if this would result in a smaller index while fulfilling the queries in a acceptable with.

7
  • 2
    Why is deleted the leading column? It's by far, presumably, the least selective.
    – Stu
    Commented Jul 16, 2023 at 17:52
  • Since every query I write includes a condition for only deleted records, shouldn't it be part of the index?
    – onassar
    Commented Jul 16, 2023 at 21:05
  • 1
    Probably not but it depends, and probably not the leading column unless it can have many values for different types of deletedness....
    – Stu
    Commented Jul 16, 2023 at 23:50
  • If about every query includes a condition on the deleted column, you may consider partitioning the table. Thus you would always either select in the deleted or undeleted part of the data and wouldn't have to include this in any index. dev.mysql.com/doc/refman/8.0/en/partitioning.html Commented Jul 17, 2023 at 7:15
  • As to the indexes. Don't combine them, as this would make the index less usable or even unusable for one of the queries. Commented Jul 17, 2023 at 7:19

1 Answer 1

0

You can use an INDEX multiple times, but only from left to right without missing a field. Example: Index (fieldA,fieldB,fieldC) then you can use this for fieldA or fieldA and fieldB or fieldA,fieldB, fieldC but not for an access to fieldA and fieldC or fieldB and fieldC.

So this doesn't work for your queries

1
  • Thanks @bernd-buffen as mentioned above, I didn't know the order (left to right) of the columns in the index had such an effect. I'll look more into this.
    – onassar
    Commented Jul 18, 2023 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.