views:

24

answers:

1

I have a simple table where I have to extract some records. The problem is that the evaluation function is a very time-consuming stored procedure so I shouldn't to call it twice like in this sentence:

SELECT *, slow_sp(row) FROM table WHERE slow_sp(row)>0 ORDER BY dist DESC LIMIT 10

First I thought in optimize like this:

SELECT *, slow_sp(row) AS value FROM table WHERE value>0 ORDER BY dist DESC LIMIT 10

But it doesn't works due "value" is not processed when the WHERE clause is evaluated.

Any idea to optimize this sentence? Thanks.

+2  A: 

What does slow_sp do? To begin optimizing the query you should add an index on dist.

To make your second query work, use HAVING instead of WHERE.

Raveren
slow_sp returns a value between 0 and 100 acording to the "importance" of a word in a text corpus. Basically access to an array of precalculated values to determine an int value.I'll try the HAVING value. Thanks.
Ivan
It's a MySQL function, right? Basicly add indexes on every column you are including in where statements. I can't help more without seeing the query in the function in question.
Raveren
It's ok raveren. I forgoten HAVING clause can help. Now it's working. Thanks!
Ivan