Skip to content
Advertisement

MariaDB Created view takes too long

I have a problem. I have a table with 6 million records in it. Every record has a column dateTime, and for my code I need the most recent 16 records in ascending order. This took too long to query directly out of the original table, so I created a view using the following query:

This means that the view only contains 16 records. Then in my code I use the view with the following query:

This query takes all (16 records) the records and puts it in ascending order, but even for these 16 rows the query takes roughly 25 seconds shown on the image below: enter image description here Is there a way to speed up this select query?

UPDATE

I created an index on the Candlestick table like @The Impaler told me to, and I am now using the following query without the view:

Here are all my indexes now: enter image description here But still after the index, this query takes about 20 – 25 seconds. What can I do to improve it?

The result of show create table Candlestick;:

Advertisement

Answer

A covering index could help.

This is the final suggested index based on the review below:

Here’s all the SQL used in the test case (fiddle):

Working test case for MariaDB and generated sample data

The following is based on a guess at the actual create table statement, but it’s useful to discuss some of the possible index issues.

Here’s the plan with the current suggested index, and some sample data:

enter image description here

Here’s the plan with the descending version of the index:

enter image description here

Here’s the suggested covering index:

enter image description here

and with your actual table, and the previous ix1 index:

enter image description here

Now with the new suggested index (with openTime DESC order):

enter image description here

Update: MariaDB seems to support the descending index syntax, but may not fully support the feature. In more recent versions of Maria (10.5, for example) the new index (ix3) is not used for this test case.

We could force the index, if that were found to be helpful:

If we look at the table after adding the index, we notice the DESC term is ignored:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement