![]() | |
![]() |
| | Thread Tools | Display Modes |
#21
| |||
| |||
|
|
Thanks all for your help - I have learned a lot. Not least that the cursor approach is much quicker than the non-cursor solution (the cursor takes about 0.003 seconds while the non-cursor solution takes 1.3 - 1.4 seconds for the same input values) |
#22
| |||
| |||
|
|
MartinH <mgheal (AT) gmail (DOT) com> wrote: Thanks all for your help - I have learned a lot. Not least that the cursor approach is much quicker than the non-cursor solution (the cursor takes about 0.003 seconds while the non-cursor solution takes 1.3 - 1.4 seconds for the same input values) I don't think this is real. In fact I have strong reason to believe that a non-cursor version will be faster (but not significantly). |
#23
| |||
| |||
|
|
On 2010-12-20 10:29, Axel Schwenke wrote: I don't think this is real. In fact I have strong reason to believe that a non-cursor version will be faster (but not significantly). Axel, can you elaborate on why you expect the non-cursor version to be faster, is it due to context switching or some sort of similar effect? |
#24
| |||
| |||
|
|
Lennart Jonsson <erik.lennart.jonsson (AT) gmail (DOT) com> wrote: On 2010-12-20 10:29, Axel Schwenke wrote: I don't think this is real. In fact I have strong reason to believe that a non-cursor version will be faster (but not significantly). Axel, can you elaborate on why you expect the non-cursor version to be faster, is it due to context switching or some sort of similar effect? I think we compare apples and pears here. The two versions below use pretty different queries, so any difference in performance is *not* a matter of using a cursor or not. |
|
It seems you use the cursor to just read the first FLOOR(Units_to_Add) matching rows. But then MySQL already has a syntax feature for that: a LIMIT clause. And of course MySQL can use LIMIT to optimize execution of a query (i.e. read rows in specified order and stop reading when the LIMIT is reached) http://dev.mysql.com/doc/refman/5.1/...imization.html A non-cursor version of that query could look like so: SELECT MAX(inner.day) FROM ( SELECT reporting_day AS day FROM reporting_days WHERE reporting_day >= Start_Date AND day_type = '' ORDER BY reporting_day LIMIT FLOOR(Units_to_Add) ) AS inner; Due to syntax limitations one had to use PREPARE and EXECUTE to put the `Units_to_Add` value into that query (aka "dynamic SQL"). |
#25
| |||
| |||
|
|
delimiter // CREATE FUNCTION Add_Working_Days3(Units_to_Add Decimal(10, 2), Start_Date Date) RETURNS date BEGIN declare res date; set @qry = 'SELECT MAX(inner.day) into res FROM ( SELECT reporting_day AS day FROM reporting_days WHERE reporting_day >= Start_Date AND day_type = '''' ORDER BY reporting_day LIMIT FLOOR(' || Units_to_Add || ') ) as INNER)'; PREPARE stmt from @qry; EXECUTE stmt; END// delimiter ; |
#26
| |||
| |||
|
|
On 2010-12-28 19:32, Axel Schwenke wrote: A non-cursor version of that query could look like so: SELECT MAX(inner.day) FROM ( SELECT reporting_day AS day FROM reporting_days WHERE reporting_day >= Start_Date AND day_type = '' ORDER BY reporting_day LIMIT FLOOR(Units_to_Add) ) AS inner; Due to syntax limitations one had to use PREPARE and EXECUTE to put the `Units_to_Add` value into that query (aka "dynamic SQL"). Can you provide a working example inside a function? |
![]() |
| Thread Tools | |
| Display Modes | |
| |