I would like to know what are the best solutions to optimize the response time of an SQL query for a table containing more than 2.000.000 records
As a solution, I thought of a virtual table by creating a SQL view (In fact, I prefer mysql to search early in the lines created this year because the data of this application is based on the season.)
Is there a better solution or recommendation?
eg to search all rent lines of rent 12
before =>
select * from rent_lines Where rent_id = 12
Now =>
I created a view
CREATE VIEW v_rent_lines
AS SELECT rent_id, category_id, customer_id, amount ..
Where rent_lines FROM created_at > = (select starts_on from seasons where current = true)
select * from v_rent_lines Where rent_id = 12
Notes:
-
database engine is being used InnoDB
-
I added indexes table (index_rent_lines_on_rent_id, index_rent_lines_on_category_id, index_rent_lines_on_customer_id)
-
rent has many rent_lines
The view is not really helping anything in this case. Views primarily help you, as a developer, by letting you refer to a more complex query by name rather than having to repeat the details all the time. They don’t really help mysql all that much.
You have a lot of options.
If you don’t yet, ensure that you have an index where rent_id is either the only field in the index, or the first field in the index. For example:
You can consider using mysql’s partitioning system and partitioning on rent_id
You can create an index that will have a lower cardinality by doing something like:
This will let you do a query like: