Answer the question
In order to leave comments, you need to log in
How to optimize a database query?
Hello, there is a site on Wordpress, I added my table to the database. The table weighs about 80 MB.
In the search field on the main page, the visitor enters the code and the code makes a request to the database through
function get_my_product( $code ) {
global $wpdb;
$table_name = $wpdb->get_blog_prefix() . 'dbcsv';
$product = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE `КОД_ТОВАРА_УКТЗЕД` = %d;", $code ) );
return $product;
}
Answer the question
In order to leave comments, you need to log in
Execute sql query with EXPLAIN, for sure you need to add an index for `PRODUCT_CODE_UKTZED`.
Strange column name)))
Since the browser hangs, it means that the selection turned out to be too large, therefore, as mentioned above, you need to display it page by page
see what your query produces. How many entries and what exactly is in them. Use special clients for this. If there is an index on `CODE_PRODUCT_UKTZED`, then the query is already optimized, you can still limit the selection to only the necessary fields.
Forget about paginated var_dump.
Try adding LIMIT 100 or 1000 to the SQL query. To limit the result to 100 or 1000 records.
This protects against errors and avoids the browser freezing during debugging.
In any case, if the query returns more than 100 records, then this is probably a fig mechanism and you can come up with something better.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question