Outils pour utilisateurs

Outils du site


informatique:sgbd:mysql

MySQL

Books

High Performance MySQL Optimization, Backups, Replication, and More (Paperback)

Tools

Myterm

Myterm: Extensible mysql command line client with pipe chaining

Tail -f table with myterm” Ever found yourself running the same query over and over again to see if a table has got new content, just waiting for that magic row to appear? The tail command will watch for new lines in a table, just like you might tail a log file in Linux.

Mysql Workbench

Tips

How to select random rows in MySQL

The easiest way to generate random rows in MySQL is to use the ORDER BY RAND() clause. This can work fine for small tables. However, for big tables, it will have a serious performance problem, as in order to generate the list of random rows, MySQL needs to assign random number to each row and then sort them.

Create DB & User

mysql> create database db_name ;
mysql> grant usage on *.* to db_user@localhost identified by ‘db_password’;
mysql> grant all privileges on db_name.* to db_user@localhost ;

A user for backup only :

CREATE USER 'backup'@'localhost' IDENTIFIED BY 'password';
REVOKE ALL  PRIVILEGES  ON  *.* FROM  'backup'@'localhost';
REVOKE GRANT  OPTION  ON  *.*  FROM  'backup'@'localhost';
GRANT SELECT, LOCK TABLES ON *.*
    TO 'backup'@'localhost' 
    WITH
        MAX_QUERIES_PER_HOUR 0
        MAX_CONNECTIONS_PER_HOUR 0
        MAX_UPDATES_PER_HOUR 0
        MAX_USER_CONNECTIONS 0 ;
FLUSH PRIVILEGES;

Reset the Root Password

$ service mysql stop
$ mysqld_safe --skip-grant-tables

dans un autre terminal:

$ mysql -u root
 UPDATE mysql.user SET Password=PASSWORD('TheNewPassword') WHERE User='root';
 exit

tuer le “mysqld_safe” avec un Ctrl^C

$ service mysql start

Procedures and Functions

Delimiter

Dans le corps d'un Trigger ou d'une Stored Procedure il peut y avoir des ';', alors il faut un nouveau séparateur pour délimiter l'ensemble. C'est DELIMITER :

DELIMITER //
CREATE TRIGGER t_bu BEFORE UPDATE ON t
   FOR EACH ROW
   BEGIN
     DECLARE CONTINUE HANDLER FOR 1264 SET new.column1 = -1;
     SET new.column1 = new.column1 * 2;
   END;//

Triggers

http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html

Quelques exemples :

  CREATE TRIGGER trigger_name trigger_time trigger_event
    ON tbl_name FOR EACH ROW trigger_stmt

wa_Guide, un nouveau tuple doit être insérer dans wa_GuideVote (table gérant les votes des visiteurs au niveau de chaque guide) avec comme idGuide l’id du guide qui a été ajouté. Avec un trigger cela donne ceci :

CREATE TRIGGER voteGuideInsert AFTER INSERT ON wa_Guide
FOR EACH ROW
INSERT INTO wa_GuideVote (idGuide) VALUES (NEW.idGuide);

Vous allez me dire que si on supprime un guide il va rester un tuple inutile dans wa_GuideVote. Et bien non ! :

CREATE TRIGGER voteGuideDelete AFTER DELETE ON wa_Guide
FOR EACH ROW
DELETE FROM wa_GuideVote WHERE idGuide = OLD.idGuide;

Un astuce pour gérer un cache de données: quand une table est mise à jour elle met à jour un timestamp dans une autre table. ceci permet de tenir des valeurs de last modification par groupe de données.

drop trigger if exists TABLE_A1_TRIGGER ;

create trigger TABLE_A1_TRIGGER AFTER INSERT ON TABLE_A1
for each row
  REPLACE INTO LAST_MOD (lm_module) values ('MyModule')

Et encore d'autres :

delimiter //
CREATE TRIGGER survey_check_empty BEFORE INSERT ON surv_surveys
FOR EACH ROW
BEGIN
IF LENGTH(NEW.survey_name) < 1 THEN
RETURN FALSE;
END IF;
END; //
delimiter ;
delimiter //
DROP TRIGGER demande_before_insert//
CREATE TRIGGER demande_before_insert BEFORE INSERT ON demande
FOR EACH ROW
BEGIN
  DECLARE i_decalage NUMERIC;
  DECLARE c_file_demandes CURSOR FOR
  SELECT decalage_sec FROM
  file_demandes,
  themes
  WHERE file_demandes.id_theme = themes.id
  AND file_demandes.id = NEW.id_file_demandes;

  # valorise la date cible si elle est vide
  IF(NEW.date_objectif='0000-00-00 00:00:00' OR NEW.date_objectif IS NULL)THEN
    OPEN c_file_demandes;
    FETCH c_file_demandes INTO i_decalage;
    CLOSE c_file_demandes;
    SET NEW.date_objectif = DATE_ADD(NEW.timestamp, INTERVAL i_decalage SECOND);
  END IF;
END;//
delimiter ;

XA Transaction

Replication

Articles:

Sans arrêter le Master avec l'option “–master-data” de mysqldump.

Performance

Optimisation.

Articles

Tips

  • Always check whether all your queries really use the indexes you have created in the tables. In MySQL, you can do this with the EXPLAIN statement. Check status variable “Select_scan”, “Select_full_join”.
  • With MyISAM tables that have no deleted rows, you can insert rows at the end at the same time that another query is reading from the table. If this is important for you, you should consider using the table in ways that avoid deleting rows. Another possibility is to run OPTIMIZE TABLE after you have deleted a lot of rows.

Comparaison InnoDB BuiltIn vs Plugin

Comparaison tpc-c entre la version embarquée de InnoDB et le plugin InnoDB sur une instance Amazon c1.xlarge et du MySQL 5.1.43. Pas de tuning particulier ⇒ +100% !!!

  • en abscisses, le temps : 20 minutes de mesure avec 10 minutes de chauffe
  • en ordonnées, le nombre de transactions

Author: GUI

informatique/sgbd/mysql.txt · Dernière modification: 05/02/2016 13:28 par cyrille