I'm looking for a way to protect my database against runaway queries issued by an application. I have control over both the application and the database. It occurs to me that I can protect against two specific things relatively easily:
- Set a
max_statement
usingGRANT ... MAX_STATEMENT_TIME
for the application user. - Set
sql_select_limit
for each connection.
When testing, I can see that if I set a max_statement_time
of 5
(seconds), if I execute a SELECT SLEEP(10)
, I end up with an error like this:
Query execution was interrupted (max_statement_time exceeded)
But if I perform a SELECT which should return a number of rows that exceeds some value (e.g. 1000), I get this result:
mysql> set sql_select_limit = 1000;
Query OK, 0 rows affected (0.000 sec)
mysql> SELECT * FROM largetable;
....
....
1000 rows in set (0.010 sec)
mysql> show warnings;
Empty set (0.000 sec)
So while I get the "protection" of limiting my query results to 1000 rows, I think what I really want is an error to be thrown in these cases, so we can identify them in the application and fix them.
Is there anything in MariaDB that is like sql_select_limit
but throws an error rather than simply limiting the size of the result set?
SELECT
of those rows at the end of the execution. If your query elicits a table scan against a 1 million row table, it's still going to have to scan 1 million rows, even if limited to return only 1,000 rows. The actualSELECT
step is the easiest part.SELECT * from bigtable
which should take essentially zero time to execute, but a long time to stream).sql_select_limit
is exactly what I have said at the end of my question: what I really want is an error to be thrown in these cases, so we can identify them in the application and fix them.