2

In my Mariadb table I have two id-columns (A,B), which combined are unique, but not individually. This table is quite large. To speed up queries like

SELECT B WHERE A=?

I have added a primary key (A,B), which worked fine. However sometimes I also need queries

SELECT A WHERE B=? 

which are rather slow. What is the optimal way improve performance here? Is it reasonable to create a second unique index (B,A) on the table?

Moreover, if I do create that second index, does inserting entries into the table become slower, since both indexes are checked? In theory, checking that (A,B) is unique implies that (B,A) is unique, but does Mariadb know that, or does it check both?

1 Answer 1

5

Is it reasonable to create a second unique index (B,A) on the table?

Yes, since your predicate in the WHERE clause in the second query is on column B and your original index that leads with column A (i.e. (A, B)) won't be able to be seeked through for this query. So you currently are probably experiencing a less than performant index scan.

Moreover, if I do create that second index, does inserting entries into the table become slower...

Yes.

...since both indexes are checked?

Not so much because of redundant checking for uniqueness, more so it's because of the additional write-overhead there is to maintain two indexes now. But generally that overhead is rather negligible unless you're writing millions of records a second, for example.

There is a general balance though between not enough indexes and too many. It depends on the query use cases, the read vs write workload, the infrastructure behind the database server, and what's acceptable runtimes all around. Typically, up to 5 indexes on a table are fine, and up to 5 columns per index (for being cognizant of index width) is fine. But it's a very loose guideline, and sometimes less or more is the correct number for a specific table's use cases.

10
  • 1
    I think the second index doesn't need to be unique since that logical constraint is already handled by the first one Commented May 10, 2023 at 12:43
  • @user20042973 Yes, probably true. Though some database systems have minor performance optimizations when reading from an index that has a unique specification defined on it. Not sure if MariaDB does, and generally it's very minor. Again, that's not really the overhead concern anyway.
    – J.D.
    Commented May 10, 2023 at 12:46
  • @user20042973 Making it unique usually does get better insert performance, but also for SELECT queries not every optimizer is able to infer uniqueness from a different index than the one being queried. Commented May 10, 2023 at 12:55
  • 1
    Interesting, the read optimizations make sense. Today I learned about potential insert performance improvements as well, I naively would have guessed the opposite. Thank you both Commented May 10, 2023 at 15:04
  • @Charlieface - No. When INSERTing, all Unique indexes must be checked before returning to the client. Non-unique secondary indexes can be delayed and performed in the background (cf the "Change buffer").
    – Rick James
    Commented May 10, 2023 at 22:18

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.