Show exact timing on SQL query - mysql

When I do the following statement, I only get two decimal places on the output:
> select * from table;
98 rows in set (0.00 sec)
How would I get more precision on this (without using SHOW PROFILES). For example, it would be nice to see:
> select * from table;
98 rows in set (0.001837884 sec)

Hey man what you want is a bit hardcore. You need to customize the nice_time() on mysql core, then you can set the decimal places you wanted.
There is a another question like this on stackoverflow, you can access this link to further information:
How do I see high-precision query times in mysql command line?

Related

Why my sql REGEXP_REPLACE is not changing all matches?

I'm using mariadb with this code:
UPDATE files SET file_source = REGEXP_REPLACE (file_source, 's:[1-9][1-9][1-9]|s:[1-9][1-9]|s:[1-9]', "s:12") WHERE type = 2;
I want to change all the columns where s: is followed by up to 3 numbers, and replace it with s:12
But when I run this it says:
Query OK, 10012 rows affected (0.118 sec)
Rows matched: 10375 Changed: 10012 Warnings: 0
I don't understand why? How I can see what didn't change and why? Is there a error in my code?
Also if I run this twice, it will say 0 changed, but from my understanding, this code should change everything again to s:12 even if it's already s:12or am I wrong?
This works fine
Update Table1 SET file_source =
REGEXP_REPLACE (file_source, 's:[0-9]?[0-9]?[0-9]', 's:12') Where TYPE = 2;
With s:1
s:31
and s:111
See https://dbfiddle.uk/?rdbms=mariadb_10.4&fiddle=506350e8fa09b7270fb5e8b46c2e2f6f

What is note-level warning in MySQL?

Okay, I understand what are errors and warnings in the context of MySQL. But what's the need of note-level warning? I have already searched the MySQL documentation but didn't find anything relevant. It would be better if someone could shed some light on the what are they and why they are useful.
mysql> create database if not exists city;
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> show warnings
-> ;
+-------+------+------------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------------+
| Note | 1007 | Can't create database 'city'; database exists |
+-------+------+------------------------------------------------+
1 row in set (0.00 sec)
I've always considered Note to be like an "FYI": something happened, or didn't, that may be of interest. The closest definition I can find in the docs is:
... events that do not affect the integrity of the reload operation
which is from the sql_notes server variable, one perhaps not often used outside of mysqldump.
Trawling through the MySQL source code, looks like Sql_Condition::SL_NOTE annotates warnings of this level. There are a few, but they are mostly as you'd expect for non-impactful information:
Event already exists
Table already exists
Query '%s' rewritten to '%s' by a query rewrite plugin
Password set
Sadly, I would have expected the code docblock to give a little more information about them, but it doesn't:
class Sql_condition {
public:
/**
Enumeration value describing the severity of the condition.
*/
enum enum_severity_level { SL_NOTE, SL_WARNING, SL_ERROR, SEVERITY_END };
This might warrant a documentation bug report to MySQL team.
Interestingly, MariaDB has this to say:
A note is different to a warning in that it only appears if the sql_notes variable is set to 1 (the default), and is not converted to an error if strict mode is enabled.
My takeaway from that, in Maria and possibly by extension MySQL: notes are warnings, but ones that can be ignored because no data-loss or side-effect is described.

Zero padding : convert MD-1 to MD-001 with pure sql

guys I need help.
I am using Mysql / phpmyadim.
I have db with table which stores name and code id of people.
+--------+---------+
| Name | code_id |
+--------+---------+
| Nazeer | MD-1 |
+--------+---------+
I have 10 contacts and ids. I am using php program which used to generate automatic code.
recently i imported more records in to db from excel file and record increase to 5000+.
My php automatic code stopped generating codes giving me syntax error on code id.
I figured out that my excel import was having code id like MD-1, MD-2, etc. and my program used automatic code for number in 3 digits since my record is over thousands which 4 digit it give syntax error.
I did some research on solving that and the answer was to change all 2 digit numbers eg. "MD-1" ~ "MD-99" TO "MD-001" ~ "MD-099" and my program will work again.
so the question is how do i do that in phpmyadmin sql to change it. I need to keep 'MD-' and add '0' then add back the corresponding number.
thanks and appreciate your help in advance.
Regrds.
this sql will update all your data, but like I said in comments, you better off fixing your php code instead.
WARNING : this sql only works assuming all your data are in the format of [MD-xxx] with 3 or less numbers in it
UPDATE your_table SET
code_id=case length(substr(code_id,4))
WHEN 1 THEN concat("MD-00",substr(code_id,4))
WHEN 2 THEN concat("MD-0",substr(code_id,4))
ELSE code_id END;
I assume that you want to update the content MD-1 to MD-001 and MD-99 to MD-099. To do that you can write a PHP code to retrieve the rows one by one and have to match patterns and then update. Here are some useful links. link 1
HINT : you can check 5 digit string and then add another 0 in the position of 3.(use [exploid] to split by "-" and then concat with "-0" 2) There are no way to do the same only by using MYSQl since it's not a programming language. And other thing is PHP is not a program. It's a programming language.
run UPDATE query and use CONCAT function :
for ($x=0; $x=<upto>; $x++){
UPDATE <table_name>
SET <columnname>= CONCAT('MD-',0,$x)
WHERE <columnname>= CONCAT('MD-',$x)
}
Below simple update command can help you.
UPDATE mytable
SET code_id=IF(LENGTH(code_id)=4,CONCAT(SUBSTRING_INDEX(code_id,'-',1),'-00',SUBSTRING_INDEX(code_id,'-',-1)),IF(LENGTH(code_id)=5,CONCAT(SUBSTRING_INDEX(code_id,'-',1),'-0',SUBSTRING_INDEX(code_id,'-',-1)),code_id));

Long detailed listing in SELECT Queries

I've had a look and reviewed the MySQL syntax, but can't figure out the right query modifier.
When you perform a normal select query, if there are a lot of columns (or one or two wide columns) the output wraps around the screen making it hard to read.
+----------------------------------------+-----------+--------+-----------------------
-----------------+----------------------------------------+------+-------------------+
--------------+---------------------------+-----------------+-------------------+-----
-------------------+-----------------+-------------------+-----------+
I recall someone showing me a long time there was a way of coercing MySQL to output each column on its own line like so:
id: 123456
Short_Field: Boy it's short
A_Long_Field: This is quite long, wow, very lorum, much ipsum...
Can anyone tell me what I'd need to put in the follow query to replicate this behaviour:
SELECT * FROM TABLE WHERE COLUMN="value" LIMIT 1;
The mode you are looking for is called "vertical output" and it is available when using \G as the delimiter in the MySQL command line client.
SELECT * FROM `table` WHERE `column` = 'value' LIMIT 1\G
Documentation on MySQL command line options

mysql version of reddit pgsql hotness function

Firstly sorry for my english, it's my first post here, and my english isn't as well as i wish but i hope it'll be enough to get a answer.
So how some of you maybe now reddit put their own source code on github and i want to use (a little modified by me) version of sql schema with a hotness algorithm. The problem is that schema is written in psgsql and my database use mysql engine.
I tried to convert schema manually but i give up with no effects, so i try again with misc tools and apps, but not even one of them support converting of procedures & functions, and the problem is that i need exactly just that one option.
So, is anyone of you can help me convert the hotness function from there:
create or replace function hot(ups integer, downs integer, date timestamp with time zone) returns numeric as $$
select round(cast(log(greatest(abs($1 - $2), 1)) + sign($1 - $2) * (date_part('epoch', $3) - 1134028003) / 45000.0 as numeric), 7)
$$ language sql immutable;
to mysql schema, i would be very grateful :)
Once again sorry for my language, i now that i underestimates the standard :)
I just wrote this function below, and it seem to work.
CREATE FUNCTION hot (ups INT(10),downs INT(10),d TIMESTAMP)
RETURNS DOUBLE DETERMINISTIC
RETURN ROUND(LOG(GREATEST(ABS(ups-downs), 1)) + SIGN(ups-downs)*(UNIX_TIMESTAMP(d) - 1134028003) / 45000.0,7);
I compared its outputs to the outputs of this Python code, everything seem OK.
Sample run:
SELECT hot(20,10,'2013-01-01 00:00:00');
Query OK, 0 rows affected (0.00 sec)
+----------------------------------+
| hot(20,10,'2013-01-01 00:00:00') |
+----------------------------------+
| 4957.1202962 |
+----------------------------------+
1 row in set (0.00 sec)
I don't know the MySQL syntax for user defined functions, but some PostgreSQL specific parts are:
date_part('epoch', $3)
Number of seconds of $3 since the epoch i.e. since 1970-01-01 00:00:00.
1134028003
Number of seconds from epoch to 2005-12-08 07:46:43.
Perhaps this is useful for finding MySQL equivalents.