Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

How can i Optimize sql Query ?

 
   Database Help (Home) -> PHP SQL RSS
Next:  speed  
Author Message
EclipSE

External


Since: Feb 20, 2005
Posts: 1



(Msg. 1) Posted: Sun Feb 20, 2005 8:40 am
Post subject: How can i Optimize sql Query ?
Archived from groups: alt>php>sql (more info?)

Hi,

I'd like to optimize this query:


Code:

SELECT *
FROM `links`
WHERE active = "1" AND mainweight != 0 ORDER BY Rand()*(1/mainweight) LIMIT
5

I have a database of links wich has 3 000 rows. I'd like to select weighted
random links from it (mainweight is the weight of the link). On my serwer
this query is executing 0.8 sec. When i have 50-100 users on my site this is
a problem.

How can i optimize/change this query ?

 >> Stay informed about: How can i Optimize sql Query ? 
Back to top
Login to vote
Steve222

External


Since: Sep 20, 2004
Posts: 4



(Msg. 2) Posted: Mon Feb 21, 2005 11:02 am
Post subject: Re: How can i Optimize sql Query ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Some suggestions: Simple optimisations...

Make sure you have an index on column `active`

Either:
Add a new column and precalculate 1/mainweight so you don't have to
calculate it in the query
Or:
Simplify the computation in the query to Rand()/mainweight

Bigger changes...

Instead of querying the whole table for each request, create a CRON job
to create a smaller (~100 row) result table and query from that on each
request.

Your visitors will not know or care that they are seeing links from the
same (smaller) subset as all other visitors, and as long as you refresh
the subset periodically the result will be similar to querying the
whole table.

---
Steve

 >> Stay informed about: How can i Optimize sql Query ? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
combobox and print query - Hi, I have problem with php and MSSQL I have a combobox with the name of 10 company and in my database i have 1 table different for each company. I need that when the user choose in the combobox one of this company appeard the table with the data. ...

Mysql query cache? - Hello. I have a strange problem with my website at work. There is a database with some product data. Data is registered thru php - website, and once a week a raport is generated from this data. If a mistake was made, the data of current week can be..

function problem involving query - I'm trying to construct a database of old houses including the parts they are made of as roof construction, floor construction, types of windows, vaults, etc. To do that I set up several tables. One containing the house id, address, last visit, etc. For....

PHPtriad v2.2.1 removal - When I fist started using apache/php/mysql I used phptriad [a wonderful piece of kit] to get me up and running. I've used this ever since. I'm think inow of 'upgrading' to laters versions of all three applications, and have found various tutorials/Ho...

MySQL regular expression to match a imploded item - Hello I need to use MySQL's REGEXP (POSIX compliant) to search registries where one field is an imploded set of integer values separated with pipes "|". I need to match one of these imploded values directly on a sql select. $sql = "SELECT...
   Database Help (Home) -> PHP SQL All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]