Tuesday 4 September 2018

The date range in the mysql view causes a slow

I have a table in mysql table with this structure

id (primary index) title, date (datetime) publicready (boolen)
and I create view like this
 CREATE view FROM SELECT * FROM tablename WHERE publicready AND date < NOW()

if I run a query against it takes 1.8 seconds but
if I remove the date range from the view the same query .0009 seconds
why is this happening and how can I fix it ?
i put an index on all columsn we are checking it did make slightly faster at 1.6

The problem with now() is that this disables MySQL from putting your query result in the cache.
If you remove the now, all selection criteria are constants and MySQL can just put the resultset of the view in the cache upon creation of the view.
With the now() that's impossible, forcing a full execution every time.

0 comments:

Post a Comment