Answer the question
In order to leave comments, you need to log in
Again about caching queries to the database, how to do it?
VPS. Website. Server side - PHP and MySQL. There is a site search. After entering 2 characters, a query is made to the database and the results are immediately uploaded under the search string. When entering more than 2 characters, the search was already done on the client side from what came from the database in order to avoid unnecessary queries.
With the client part, everything is good, everything suits the speed. But the query to the database does not suit, about 2 seconds is a long time, the user will not type two characters first, then wait, then the rest.
The search goes through different tables: categories, subcategories, products, users, etc. Along the way, queries are made to related tables, for example, countries, cities, etc., so that later users, in the results of satisfying rows, substitute the name of the country and city, for example.
Standard queries:
$resultCat = $mysqli->query("SELECT `id`,`title` FROM `cat` WHERE `title` LIKE '%".$query."%' ORDER BY `title`");
$resultCityList = $mysqli->query("SELECT `id`,`title` FROM `city` ORDER BY `title`");// Вытаскиваем все города
$resCity = $mysqli->query("SELECT SQL_CACHE `id`,`id_country`,`title` FROM `city` ORDER BY `title`");
Answer the question
In order to leave comments, you need to log in
We remember the second main programming rule: if it works slowly, then you don’t need to cache anything. It is necessary to make everything work quickly without any cache.
first: WHERE `title` LIKE '%".$query."%'
- remove the full wildcard, LIKE '".$query."%'
it will work faster and use the index, unlike the first option. You do not need to choose "Kaliningrad" by "grad".
The second - indexes are longer on text fields, where it is placed in a varchar - make a varchar with an index. Then most likely the cache is not needed.
without delving into the intricacies of the implementation ...
it will be more profitable to cache the result by query) caching efficiency itself is calculated as %% cache hit.
but the effect of the cache / its absence on user metrics should not be allowed. everything should work without the cache
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question