Welcome Guest, Not a member yet? Register   Sign In
Help tuning a very poor SQL query
#2

[eluser]TheFuzzy0ne[/eluser]
My first reaction is to suggest you don't order by RAND(). It actually re-orders the rows, which can be slow in itself. Instead, I'd suggest you count the rows, and do something like this:
Code:
$this->db->where('grade', $grade);
$row_count = $this->db->count_results('kg_compounds');

$this->db->where('grade', $grade);
$this->db->limit(1, rand(0, $row_count-1));
$res = $this->db->get('kg_compounds');

It's two queries, but I'd be interested to know how much of a difference it makes (if any).

Sorry, I know I've totally avoided your question, but I am about to dash off out the door, so I thought I should point that out.


Messages In This Thread
Help tuning a very poor SQL query - by El Forum - 06-17-2009, 08:03 AM
Help tuning a very poor SQL query - by El Forum - 06-17-2009, 08:19 AM
Help tuning a very poor SQL query - by El Forum - 06-17-2009, 08:24 AM
Help tuning a very poor SQL query - by El Forum - 06-17-2009, 09:42 AM
Help tuning a very poor SQL query - by El Forum - 06-17-2009, 09:47 AM
Help tuning a very poor SQL query - by El Forum - 06-17-2009, 10:10 AM
Help tuning a very poor SQL query - by El Forum - 06-17-2009, 10:28 AM
Help tuning a very poor SQL query - by El Forum - 06-17-2009, 11:21 AM
Help tuning a very poor SQL query - by El Forum - 06-17-2009, 11:40 AM
Help tuning a very poor SQL query - by El Forum - 06-17-2009, 11:49 AM
Help tuning a very poor SQL query - by El Forum - 06-18-2009, 05:49 AM
Help tuning a very poor SQL query - by El Forum - 06-18-2009, 05:53 AM
Help tuning a very poor SQL query - by El Forum - 06-18-2009, 06:02 AM
Help tuning a very poor SQL query - by El Forum - 06-18-2009, 09:57 AM
Help tuning a very poor SQL query - by El Forum - 06-18-2009, 10:14 AM



Theme © iAndrew 2016 - Forum software by © MyBB