简体   繁体   中英

How can I better optimize my PHP and MySQL Queries?

I have a website I'm working on and it all works just fine. I'm concerned that under load the way I am doing things currently could cause some server lag.

In the database I have a table with a lot of information in it (14+ columns and several hundred entries). The PHP page has a script on it to generate page links (Page 1 of 6, next page, previous, first, last, etc.). It currently displays 25 entries per page. The script multiplies the page number by 25 to get which entry in the table it needs to start on for the select.

That's not too horrible I don't think. The real issue comes from the "Page x of y". Meaning that I need to know how many pages of items there will be, or how many times 25 goes into the number of entries. I've done this by using something like:

SELECT count(id) FROM `footwear`

to just get the count of entries in the table. Then using that count I can calculate all my pages. Is that the most efficient way of doing things? Or perhaps there's a better way to calculate the page count?

Just make sure you have an index on id and it'll be very fast in MySQL.

If you suspect that it's slow you can always measure it to calm your mind.

If you are using MyISAM, then you are fine, if you are using InnoDB as your storage engine, you might want to cache the number of rows or store it on a separate table, the reason for that is that MyISAM will keep record of the number of rows on the table but InnoDB will count all the rows everytime you do the query.

I hope this helps.

I might recommend query caching in MySQL to improve general performance.

Here's a guide to enable that: http://www.cyberciti.biz/tips/enable-the-query-cache-in-mysql-to-improve-performance.html

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