I need to display the most viewed news for the last week. The request is working, but it takes about a minute. The database has more than one and a half million entries.
How can I optimize a query, can there be an option to use another query?
The index is on all fields. Index city_url - unique items 599 index time - unique items 1633941 index views - unique items 15
SELECT * FROM news WHERE city_url like '$city' AND time > unix_timestamp(date_sub(current_date, interval 1 week)) ORDER BY views DESC limit 3
%sign, then the index cannot be used. And show the execution plan of the query, which is obtained by theexplaincommand and from which to start any optimization - Mike(city_url, time)would be optimal and writeorder by city_url, views DESCthat it could not use an index on views. However, this index is best to delete at all, 1/15 of the table for it is still a huge amount and you are unlikely to perform the search where views = X, so it is not applicable in any cases and only hurts this particular one - Mike=in the request, this can also confuse the optimizer - Mikecity_url LIKE 'moskva'is a horror. Rewrite oncity_url = 'moskva'. 2. TryALTER TABLE bezf ADD KEY idx_c(time, city_url, views);thenEXPLAIN SELECT ...and exhaust here. To correctly measure the speed,SELECT SQL_NO_CACHE ...- there will be an “honest” time, and not how much muscle climbed into the cache. What time was "before" optimization, and "after"? 3. Stop throwing shots, issue in the form of text. To do this easily, use the muscle console (mysql ...) - Total Pusher