user2701926
user2701926

Reputation: 31

problems due to large number of sql queries in a php page

i am working on a project where i need to put large number of sql queries on a single page .. my question is that is there any problem that i will be having in future if my site gets heavy traffic ... i do not want my site to slow down.. please suggest some way so that the number of queries does not affect my site performance..

i am working on php

sql query may look like

$selectcomments=mysql_query("select `comment`,`email`,`Date` from `fk_views` where (`onid`='$idselect_forcomments' and comment !='') order by Date asc");

Upvotes: 1

Views: 172

Answers (3)

InsaneCoder
InsaneCoder

Reputation: 8268

Yes, obviously if you have a lot of queries on a single page then even in moderate traffic it can flood your database with queries.

Few Tips: 1)You should work on your database structure,how you have created tables,which table stores what,normalization etc.Try to optimise storage and retrieval of information so that in a single query,you fetch max. information.This will reduce the calls to database.

2)Never store and fetch redundant info (like age,that you can calculate from DOB) from database.

3)Pagination (as pointed earlier).

4)Caching

5)If you are updating small portions on your page at a time,then instead of loading entire page,use AJAX to update necessary portions.It will also increase interactivity.

Upvotes: 0

user529543
user529543

Reputation:

One thing what you can do is Memcached. It will store in cache those results. So for the next visitor, who chick on the same page will read a cached objects from sql not need to run again.

Other trick: order by Date asc if you have huge result, than better, faster to do it in PHP side, those can rally slow down the query if they need to do a full table scan.

Other like Yannik told: pagination ( this is basic, ofc ) and divide pages.

You can speed up the delay from pagination with pre-executing sql with Ajax: get count of total results for pagination.

Upvotes: 0

Yannick Bloem
Yannick Bloem

Reputation: 147

Of course, if your site gets bigger, you will have problems putting everything on one page. That's logic, and you can't change it.

Different solutions:

It's obvious that if your database gets too big, it'll be impossible to simply dump all the data on one page. Even the quickest browsers would crash.

Upvotes: 1

Related Questions