I have a table baseTable
with columns id
and dateCol
. I want to create several tables where I group by a function of dateCol
.
My first attempt at this was
CREATE derivedTable ...
INSERT INTO derivedTable
SELECT id, myFunc(dateCol) AS datePrime, ...
FROM baseTable
GROUP BY id, datePrime
This worked! But it's pretty slow. My guess is that it's slow because there's no index on the datePrime
column. I thought I'd just add a quick index on datePrime
to validate this theory, but this turned out to be much harder than I was expecting.
My first instinct was to add a datePrime
column to baseTable
with
ALTER TABLE baseTable ADD COLUMN datePrime INT GENERATED ALWAYS AS (myFunc(dateCol));
Then I could add an index on that new column:
CREATE INDEX idx on baseTable(datePrime);
and finally, (hopefully) make derivedTable
quicker:
CREATE derivedTable ...
INSERT INTO derivedTable
SELECT id, datePrime, ...
FROM baseTable
GROUP BY id, datePrime
However, MySQL doesn't allow you to use stored functions to derive a column (link):
Generated column expressions must adhere to the following rules... Stored functions and loadable functions are not permitted.
The only thing I can think of right now to create a new table baseTablePrime
which contains all the columns from baseTable
plus a new datePrime
column created as
CREATE TABLE baseTablePrime AS SELECT id, dateCol, myFunc(dateCol), ... FROM baseTable
I think this would work, but then I have baseTable
and a slightly augmented baseTablePrime
, which seems wasteful and inelegant.
Before charging ahead with what seems like at best a hack, I'm asking for help on how I should be approaching this problem. I feel like I may be attempting an SQL anti-pattern somehow. Let's take as a given that I would like to create an index on a stored column which is derived from another column using a user-defined function. What's the cleanest way to do something like this?