How many GET_LOCKs can a MySQL handle? - mysql

How many GET_LOCKS can be handled by a mysql server - by the whole server. I wasn´t able to find anything about its limitations.

As per MySQL documentation GET_LOCK() you cannot hold more than one lock per connection.
As it says
If you have a lock obtained with GET_LOCK(), it is released when you
execute RELEASE_LOCK(), execute a new GET_LOCK(), or your connection
terminates (either normally or abnormally).
So essentially, it depends on No.Of connection. I would say the equation would
No.of GET_LOCK handled = NO.Of Connections handled
I see there is a bug logged where people suggested to have concurrent lock per connection. See here http://bugs.mysql.com/bug.php?id=1118

mysql> SELECT GET_LOCK('s',10);
+------------------+
| GET_LOCK('s',10) |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT GET_LOCK('t',10);
+------------------+
| GET_LOCK('t',10) |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT GET_LOCK('b',10);
+------------------+
| GET_LOCK('b',10) |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT IS_FREE_LOCK('b');
+-------------------+
| IS_FREE_LOCK('b') |
+-------------------+
| 0 |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT IS_FREE_LOCK('t');
+-------------------+
| IS_FREE_LOCK('t') |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT IS_FREE_LOCK('s');
+-------------------+
| IS_FREE_LOCK('s') |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.00 sec)

Related

Tab completion in mysql command line

I tried to test mysql's functions with select from command line but find that tab completion does not work
mysql> select Now() as current_moment;
+---------------------+
| current_moment |
+---------------------+
| 2019-12-04 22:11:11 |
+---------------------+
1 row in set (0.00 sec)
mysql> select current_time;
+--------------+
| current_time |
+--------------+
| 22:11:27 |
+--------------+
1 row in set (0.00 sec)
If not reference to the manual, it seems very hard to play around with functions without the assistance of tab completion.
How could activate the tab completion.

Where can I use 'GET_LOCK'?

Who can share practical examples of the use of the GET_LOCK function?
Example function:
mysql> SELECT GET_LOCK('b',10);
+------------------+
| GET_LOCK('b',10) |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT IS_FREE_LOCK('b');
+-------------------+
| IS_FREE_LOCK('b') |
+-------------------+
| 0 |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT RELEASE_LOCK('b');
+-------------------+
| RELEASE_LOCK('b') |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT IS_FREE_LOCK('b');
+-------------------+
| IS_FREE_LOCK('b') |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.00 sec)
Thank you.
Resource locking, eg execute only once in some distributed systems.
For example here's tiny app which uses it: https://gist.githubusercontent.com/karnauskas/f6dd0a8608c9a335e17890d3755d5d5a/raw/6bdfa36d93ac1f171ccf0ba5152aa8765c052a96/mysql_lock.go
Implementing distributed locking, as described in this article.
Essentially, if you need to restrict access to some resource X for programs P1, P2, P3, ..., then each program needs to call GET_LOCK("some-agreed-on-string") and only the one which got 1 in response proceeds.

Translation of the word to the next line in the mysql console

mysql>SELECT LENGTH("boo
m"); //5 (I want 4)
How should I translate words on the next line without increasing the value of the number of characters?
Do like this:
mysql> SELECT LENGTH("boo"
-> "m");
+-------------------+
| LENGTH("boo"
"m") |
+-------------------+
| 4 |
+-------------------+
1 row in set (0.00 sec)
To understand this see this example:
mysql> SELECT "boo" "m";
+------+
| boo |
+------+
| boom |
+------+
1 row in set (0.00 sec)
You can see same behavior in many programming languages.

MySQL LIKE vs LOCATE

Does anyone know which one is faster:
SELECT * FROM table WHERE column LIKE '%text%';
or
SELECT * FROM table WHERE LOCATE('text',column)>0;
Added April 20th, 2015: Please read also Hallie's answer below
First one but marginally. Mostly because it doesn't have to do an extra > 0 comparison.
mysql> SELECT BENCHMARK(100000000,LOCATE('foo','foobar'));
+---------------------------------------------+
| BENCHMARK(100000000,LOCATE('foo','foobar')) |
+---------------------------------------------+
| 0 |
+---------------------------------------------+
1 row in set (3.24 sec)
mysql> SELECT BENCHMARK(100000000,LOCATE('foo','foobar') > 0);
+-------------------------------------------------+
| BENCHMARK(100000000,LOCATE('foo','foobar') > 0) |
+-------------------------------------------------+
| 0 |
+-------------------------------------------------+
1 row in set (4.63 sec)
mysql> SELECT BENCHMARK(100000000,'foobar' LIKE '%foo%');
+--------------------------------------------+
| BENCHMARK(100000000,'foobar' LIKE '%foo%') |
+--------------------------------------------+
| 0 |
+--------------------------------------------+
1 row in set (4.28 sec)
mysql> SELECT ##version;
+----------------------+
| ##version |
+----------------------+
| 5.1.36-community-log |
+----------------------+
1 row in set (0.01 sec)
+1 to #Mchl for answering the question most directly.
But we should keep in mind that neither of the solutions can use an index, so they're bound to do table-scans.
Trying to decide between a cloth or plastic adhesive bandage is kind of silly, when you're trying to patch the hull of the Titanic.
For this type of query, one needs a full-text search index. Depending on the size of the table, this will be hundreds or thousands of times faster.
I did some tests as Mchi did.And I think it's hard to say which one is faster. It looks like depending on the first occurrence of the substring.
mysql> select benchmark(100000000, 'afoobar' like '%foo%');
+----------------------------------------------+
| benchmark(100000000, 'afoobar' like '%foo%') |
+----------------------------------------------+
| 0 |
+----------------------------------------------+
1 row in set (9.80 sec)
mysql> select benchmark(100000000, locate('foo', 'afoobar'));
+------------------------------------------------+
| benchmark(100000000, locate('foo', 'afoobar')) |
+------------------------------------------------+
| 0 |
+------------------------------------------------+
1 row in set (8.08 sec)
mysql> select benchmark(100000000, 'abfoobar' like '%foo%');
+-----------------------------------------------+
| benchmark(100000000, 'abfoobar' like '%foo%') |
+-----------------------------------------------+
| 0 |
+-----------------------------------------------+
1 row in set (10.55 sec)
mysql> select benchmark(100000000, locate('foo', 'abfoobar'));
+-------------------------------------------------+
| benchmark(100000000, locate('foo', 'abfoobar')) |
+-------------------------------------------------+
| 0 |
+-------------------------------------------------+
1 row in set (10.63 sec)
mysql> select benchmark(100000000, 'abcfoobar' like '%foo%');
+------------------------------------------------+
| benchmark(100000000, 'abcfoobar' like '%foo%') |
+------------------------------------------------+
| 0 |
+------------------------------------------------+
1 row in set (11.54 sec)
mysql> select benchmark(100000000, locate('foo', 'abcfoobar'));
+--------------------------------------------------+
| benchmark(100000000, locate('foo', 'abcfoobar')) |
+--------------------------------------------------+
| 0 |
+--------------------------------------------------+
1 row in set (12.48 sec)
mysql> select ##version;
+------------+
| ##version |
+------------+
| 5.5.27-log |
+------------+
1 row in set (0.01 sec)

Automatically trimming extra zeros in mysql

mysql> select 28/4;
+--------+
| 28/4 |
+--------+
| 7.0000 |
+--------+
1 row in set (0.00 sec)
Is there a way I can set something system-wide to be more clever about this, or at least to automatically round every float to 2 places?
That's controlled by the div_precision_increment variable:
http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_div_precision_increment
MySQL> select ceiling(28/4);
+---------------+
| ceiling(28/4) |
+---------------+
| 7 |
+---------------+
1 row in set (0.00 sec)
MySQL> select format(28/4,0);
+----------------+
| format(28/4,0) |
+----------------+
| 7 |
+----------------+
1 row in set (0.00 sec)
I GOT IT !!!
set div_precision_increment = 0;
MySQL> select 28/4;
+------+
| 28/4 |
+------+
| 7 |
+------+
1 row in set (0.00 sec)
You could also set it globally:
set global div_precision_increment = 0;
or add this to /etc/my.cnf
div_precision_increment=0
I think your answer, if it exists, lies on this page
I have to be honest, I read through the rules about how precision is applied and don't really understand how you end up with 5 digits of precision by diving 28 by 4.
Good luck!