I have a query:
SELECT * FROM tb WHERE id=1
AND
sleep(CAST((SELECT 'a' REGEXP '^[n-z]') AS signed) * 15);
I’m trying to understand how it works.
1) SELECT * FROM tb WHERE id=1 # that’s simple no explanation needed
2) sleep(CAST((SELECT 'a' REGEXP '^[n-z]') AS signed) * 15); # Here it’s a knotty problem
for me.
I believe things are: sleep for (the result of –> CAST((SELECT ‘a’ REGEXP ‘^[n-z]’) AS signed) multiplied by 15.
My concerns are:
-
CAST is a function to convert (for instance from string to date) but here it is used to convert a string in a signed number ..is that correct?
-
REGEXP, I believe is used this way:
(SELECT 'a' REGEXP '^[n-z]')returns 0 (zero) so multiplied by 15 is always 0. But(SELECT 'p' REGEXP '^[n-z]')returns 1 so in this case the DBMS sleeps for 15 sec.
So, now the final question is:
- if tb has only one record whose name is ‘bob’, is the database going to pause for 15 sec?
No, with this query the database is never gonna pause because
CAST((SELECT 'a' REGEXP '^[n-z]') AS signed) * 15always returns 0, totally independent from your records: It does not use any column from the table, ‘a’ is a string literal.But other than that, you analyzed it correct. The query as you shown it does not make any sense.