D
D
Dmitry2017-04-20 10:40:21
PHP
Dmitry, 2017-04-20 10:40:21

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;
}

As a result, you need to display the value of all rows of the database in which the value of the `CODE_PRODUCT_UKTZED` column
is equal to the entered one.
When requesting, an error occurs, or rather, I get an array, when outputting it through var_dump(), the browser hangs.
I'm new at this. Could you tell me how to optimize the query to display the required information normally?
Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Evgeny Bukharev, 2017-04-20
@evgenybuckharev

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

E
Edward, 2017-04-20
@edb

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.

W
WordPress WooCommerce, 2017-04-20
@maxxannik

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 question

Ask a Question

731 491 924 answers to any question