SELECT
concat(date(created_at), ' ', LPAD(hour(created_at), 2, '0'), '-', LPAD(IF(hour(created_at)=23, 0, hour(created_at)+1), 2, '0')) as time_slice,
count(*) as '# entries'
FROM entries
GROUP BY time_slice
order by time_slice desc
limit 72;
ORDER BY
IF(CAST(orderNum AS SIGNED) != 0, LPAD(orderNum, 10, '0'), orderNum)
SELECT shopping_carts.id FROM shopping_carts
LEFT JOIN shopping_cart_items ON shopping_carts.id=shopping_cart_items.shopping_cart_id
WHERE shopping_cart_items.shopping_cart_id IS NULL;
UPDATE TABLEA a
JOIN TABLEB b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something];
DELETE `t1`
FROM `t1`
JOIN `t2`
ON `anotherstring1` = 'putin' AND `anotherstring2` = 'vodka'
AND `t1`.`sameint` = `t2`.`sameint`
SELECT a.firstname, a.lastname, a.address
FROM list a
INNER JOIN list b ON a.address = b.address
WHERE a.id <> b.id
a) Using LEFT JOIN/IS NULL:
DELETE b FROM TargetTbl b
LEFT JOIN SourceTbl a ON a.id = b.foreign_key
WHERE a.id IS NULL
b) Using NOT EXISTS:
DELETE FROM TargetTbl
WHERE NOT EXISTS(SELECT NULL
FROM SourceTbl a
WHERE a.id = foreign_key)
c) Using NOT IN:
DELETE FROM TargetTbl
WHERE foreign_key NOT IN (SELECT a.id
FROM SourceTbl a)
Warning: Whenever possible, perform DELETEs within a transaction (assuming supported - IE: Not on MyISAM) so you can use rollback to revert changes in case of problems.
USE myDB;
DROP TABLE IF EXISTS sessions2;
CREATE TABLE sessions2 LIKE sessions;
RENAME TABLE sessions TO sessions_backup, sessions2 TO sessions;
INSERT INTO `sessions`
SELECT id, session_id, data, created_at, updated_at
FROM `sessions_backup`
WHERE (created_at<>updated_at) AND (updated_at > NOW() - INTERVAL 10 HOUR);
--DROP TABLE IF EXISTS sessions_backup;
*/30 * * * * cd /home/user/wwwroot/current; /path/to/clear_expired_sessions.sh >/dev/null 2>&1
SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';
SELECT @@character_set_database;
SELECT SCHEMA_NAME 'database', default_character_set_name 'charset', DEFAULT_COLLATION_NAME 'collation' FROM information_schema.SCHEMATA;
alter table `table1` convert to character set utf8 collate utf8_unicode_ci;
#For Schemas:
SELECT default_character_set_name FROM information_schema.SCHEMATA
WHERE schema_name = "schemaname";
#For Tables:
SELECT CCSA.character_set_name, T.table_name FROM information_schema.`TABLES` T,
information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
WHERE CCSA.collation_name = T.table_collation
AND T.table_schema = "myDB";
#For Columns:
SELECT character_set_name FROM information_schema.`COLUMNS`
WHERE table_schema = "schemaname"
AND table_name = "tablename"
AND column_name = "columnname";
CREATE TABLE `post_tags` (
#...
`tags` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`published_at` datetime NOT NULL,
FULLTEXT KEY `tags_search` (`tags`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin;
SELECT * FROM post_tags
WHERE MATCH(tags)
AGAINST('venezuela' IN NATURAL LANGUAGE MODE);
-- function by Jason Rust
-- Stolen at: http://codejanitor.com/wp/2007/02/10/levenshtein-distance-as-a-mysql-stored-function/
DELIMITER $$
DROP FUNCTION IF EXISTS `levenshtein` $$
CREATE FUNCTION `levenshtein`(s1 VARCHAR(255), s2 VARCHAR(255)) RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
DECLARE s1_char CHAR;
-- max strlen=255
DECLARE cv0, cv1 VARBINARY(256);
SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2), cv1 = 0x00, j = 1, i = 1, c = 0;
IF s1 = s2 THEN
RETURN 0;
ELSEIF s1_len = 0 THEN
RETURN s2_len;
ELSEIF s2_len = 0 THEN
RETURN s1_len;
ELSE
WHILE j <= s2_len DO
SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
END WHILE;
WHILE i <= s1_len DO
SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)), j = 1;
WHILE j <= s2_len DO
SET c = c + 1;
IF s1_char = SUBSTRING(s2, j, 1) THEN
SET cost = 0; ELSE SET cost = 1;
END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
IF c > c_temp THEN SET c = c_temp; END IF;
SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
IF c > c_temp THEN
SET c = c_temp;
END IF;
SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
END WHILE;
SET cv1 = cv0, i = i + 1;
END WHILE;
END IF;
RETURN c;
END $$
DROP FUNCTION IF EXISTS `levenshtein_ratio` $$
CREATE FUNCTION levenshtein_ratio( s1 VARCHAR(255), s2 VARCHAR(255) )
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, max_len INT;
SET s1_len = LENGTH(s1), s2_len = LENGTH(s2);
IF s1_len > s2_len THEN
SET max_len = s1_len;
ELSE
SET max_len = s2_len;
END IF;
RETURN ROUND((1 - LEVENSHTEIN(s1, s2) / max_len) * 100);
END $$
DELIMITER ;
SELECT CONCAT(table_schema, '.', table_name) 'Table',
CONCAT(ROUND(table_rows / 1000000, 2), 'M') rows,
CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') DATA,
CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') idx,
CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') total_size,
ROUND(index_length / data_length, 2) idxfrac
FROM information_schema.TABLES
#where table_schema=''
ORDER BY data_length + index_length DESC
LIMIT 10;
SELECT AUTO_INCREMENT - 1 as CurrentId FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '' AND TABLE_NAME = ''
SHOW PROCESSLIST;
select ID, TIME, STATE , INFO from information_schema.processlist where USER='root' AND COMMAND!='Sleep' order by TIME desc limit 5;
-- dump stale connections:
select concat('KILL ',id,';') from information_schema.processlist where user='root';
-- dump stale connections to file:
select concat('KILL ',id,';') from information_schema.processlist where user='root' into outfile '/tmp/a.txt';
SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST
#SELECT concat('kill ', id, ';') FROM INFORMATION_SCHEMA.PROCESSLIST
where
#info is not null
#host like '10.%' and
Time>300
ORDER BY Time DESC
limit 500;
#show full processlist;
show processlist;
show open tables WHERE In_use > 0;
show variables like "%timeout%";
# SET GLOBAL wait_timeout=300;
SHOW GLOBAL STATUS;
#show full processlist;
show processlist;
show open tables WHERE In_use > 0;
#kill 503706;
show variables like 'innodb_lock_wait_timeout';
# http://stackoverflow.com/a/15252722/2840001
#SELECT @@GLOBAL.tx_isolation, @@tx_isolation, @@session.tx_isolation;
#SET tx_isolation = 'READ-COMMITTED';
# as root:
#SHOW ENGINE INNODB STATUS;
#SELECT * FROM `information_schema`.`innodb_trx` ORDER BY `trx_started`;
#SELECT * FROM `information_schema`.`innodb_locks`;
#SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;
#SET GLOBAL tx_isolation = 'READ-COMMITTED';
# Also try use SELECT FOR UPDATE only in if necesary.
CREATE DATABASE `new_db_name` CHARACTER SET utf8 COLLATE utf8_general_ci;
mysqldump --default-character-set=utf8 -h HOST -u USER -p'PASS' db_name | mysql --default-character-set=utf8 -h HOST -u USER -p'PASS' new_db_name
UPDATE TableA
INNER JOIN db2.TableA as TableB ON TableA.id = TableB.id
SET TableA.col1 = TableB.col1;
-- WARNING!!! As always, you may want to take a backup before running something like this...
-- If you want to keep the row with the lowest id value:
DELETE FROM NAMES
WHERE id NOT IN (SELECT *
FROM (SELECT MIN(n.id)
FROM NAMES n
GROUP BY n.name) x)
-- If you want the id value that is the highest:
DELETE FROM NAMES
WHERE id NOT IN (SELECT *
FROM (SELECT MAX(n.id)
FROM NAMES n
GROUP BY n.name) x)
-- A really easy way to do this is to add a UNIQUE index on the 3 columns. When you write the ALTER statement, include the IGNORE keyword. Like so:
ALTER IGNORE TABLE jobs
ADD UNIQUE INDEX idx_name (site_id, title, company);
-- This will drop all the duplicate rows. As an added benefit, future INSERTs that are duplicates will error out.
#show VARIABLES like 'slow_query_log'
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 0.8;
Select * from mysql.slow_log;
Select * from mysql.general_log;
SELECT ...
DO SLEEP(5);
SELECT ...
ALTER TABLE `comments`
ADD COLUMN `metadata` JSON NULL AFTER `updated_at`, ALGORITHM=INSTANT;
RENAME TABLE tbl TO tbl_BAK, tbl_NEW TO tbl;
INSERT INTO import_test_new
SELECT * FROM import_test
on duplicate key update
data=values(data)
SET profiling = 1;
CREATE TABLE t (i INT) ENGINE = MEMORY;
SET profiling = 0;
SHOW PROFILE CPU FOR QUERY 1;
Think twice before doing it! Do you really have very recent DB backups?
SET FOREIGN_KEY_CHECKS = 0;
SET GROUP_CONCAT_MAX_LEN=32768;
SET @tables = NULL;
SELECT GROUP_CONCAT('`', table_name, '`') INTO @tables
FROM information_schema.tables
WHERE table_schema = (SELECT DATABASE());
SELECT IFNULL(@tables,'dummy') INTO @tables;
#SELECT @tables;
SET @tables = CONCAT('DROP TABLE IF EXISTS ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;
Warning: use at your own risk!!