I've got an index on columns a VARCHAR(255), b INT
in an InnoDB table. Given two a,b
pairs, can I use the MySQL index to determine if the pairs are the same from a c program (i.e. without using a strcmp
and numerical comparison)?
- Where is a MySQL InnoDB index stored in the file system?
- Can it be read and used from a separate program? What is the format?
- How can I use an index to determine if two keys are the same?
Note: An answer to this question should either a) provide a method for accessing a MySQL index in order to accomplish this task or b) explain why the MySQL index cannot practically be accessed/used in this way. A platform-specific answer is fine, and I'm on Red Hat 5.8.
Below is the previous version of this question, which provides more context but seems to distract from the actual question. I understand that there are other ways to accomplish this example within MySQL, and I provide two. This is not a question about optimization, but rather of factoring out a piece of complexity that exists across many different dynamically generated queries.
I could accomplish my query using a subselect with a subgrouping, e.g.
SELECT c, AVG(max_val)
FROM (
SELECT c, MAX(val) AS max_val
FROM table
GROUP BY a, b) AS t
GROUP BY c
But I've written a UDF that allows me to do it with a single select, e.g.
SELECT b, MY_UDF(a, b, val)
FROM table
GROUP by c
The key here is that I pass the fields a
and b
to the UDF, and I manually manage a,b
subgroups in each group. Column a
is a varchar, so this involves a call to strncmp
to check for matches, but it's reasonably fast.
However, I have an index my_key (a ASC, b ASC)
. Instead of checking for matches on a and b manually, can I just access and use the MySQL index? That is, can I get the index value in my_key for a given row or a,b
pair in c (inside the UDF)? And if so, would the index value be guaranteed to be unique for any value a,b
?
I would like to call MY_UDF(a, b, val)
and then look up the mysql index value (a,b)
in c from the UDF.