简体   繁体   中英

How to speed up PDO query

I have products database table(tabl_products) with more than 55 millions of rows(products) and iam using PDO query to display each prduct from tabl_products table, So here is my query table structre and select query..

id | pr_name | pr_description | pr_qntty | pr_price


$pr_id = (int)$_GET['id'];
$select_pr = $con->prepare("SELECT pr_name, pr_description, pr_price FROM tabl_products WHERE id=:pr_id");
$select_pr->bindParam(":pr_id", $pr_id);
$select_pr->execute();
if(!($select_pr->rowCount()))
{
   include '404.php'; exit();
}
$fetch_pr = $select_pr->fetch(PDO::FETCH_ASSOC);

echo $pr_name = $fetch_pr['pr_name'].'<br/>';
echo $pr_description = $pr_description['pr_description'].'<br/>';
echo $pr_price = $fetch_all['pr_price'];

Above query will take 2 to 3 minutes to display a single product information.

So how can I display a single product information within 5 to 10 seconds?

(from comment)

CREATE TABLE IF NOT EXISTS tabl_products (
    id int(11) NOT NULL AUTO_INCREMENT, 
    pr_name varchar(255) NOT NULL, 
    pr_description varchar(255) NOT NULL, 
    pr_qntty varchar(255) NOT NULL, 
    pr_price varchar(255) NOT NULL, 
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I recreated similar database using MySQL 5.7.20, filled about 55000000 of rows with dummy data, and query like this:

SELECT * FROM stackoverflow_test.tabl_products WHERE id=12345678;

returns result very quickly on my (decent) PC (shows 0.000s in MYSQL Workbench).

I can give you a few ideas that can make you identify the problem. First, there are a few bugs in your code.

echo $pr_name = $fetch_pr['pr_name'].'<br/>';
echo $pr_description = $pr_description['pr_description'].'<br/>';
echo $pr_price = $fetch_all['pr_price'];

$pr_description should be replaced with $fetch_pr

$fetch_all should be replaced with $fetch_pr

Maybe there is something else in your php file that makes execution so slow. Try to execute minimal file, or use MySQL Workbench (or similar tool) to make sure it's not the issue with http server or php (maybe the query runs fast, but fetching is slow, or there are other problems?).

Especially with such big database you need to make sure indexes are used. In MySQL you can do it by adding EXPLAIN ( https://dev.mysql.com/doc/refman/5.7/en/explain-output.html ) in front of your query, ie:

EXPLAIN SELECT * FROM stackoverflow_test.tabl_products WHERE id=12345678;

then analyze fields possible_keys and key , which should include name of your index (PRIMARY). If MySQL is not using index, then adding USE INDEX (`PRIMARY`) ( https://dev.mysql.com/doc/refman/5.7/en/index-hints.html ) statement to your query should help, but you should investigate why it isn't used by default.

FYI when I added IGNORE INDEX (`PRIMARY`) just to check query execution time without index, the query runs for about 30 seconds instead of a fraction of second.

These is also a possiblility that the server is overloaded or not powerfull enough, but with index your query should be very fast anyway.

User Your Common Sense suggested that even if index is used it may be too big to fit into memory, so it is read from disk which is slow. You may want to check the size of index and compare it with available memory and (if required) adjust MySQL memory settings accordingly.

As your id is unique (primary key), you can add LIMIT 1 . Because, otherwise, even when your product is found once, PDO will continue to look other products with that particular id.

So your query becomes :

SELECT pr_name, pr_description, pr_price FROM tabl_products WHERE id=:pr_id LIMIT 1

Try and tell us how long it takes :)

PS: it is used by Doctrine ORM for example:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM