SlideShare a Scribd company logo
1 of 18
Download to read offline
Notiuni avansate Mysql
Notiuni avansate MySQL



           Notiuni avansate MySQL
    Mysql
    Tabele
    Indecsi
    Foreign key-uri
    Triggere
    Proceduri stocate
    VIEWS
    DESCRIBE
    Slow query log
    Query cache
    Performance



Mihai Oaida <mihai.oaida@gmail.com>   01
Notiuni avansate MySQL



                                      MySQL
    3.23       -   vechi
    4.1x       -   stabil, putine feature-uri
    5.0x       -   GA
    5.1       -    RC
    6.0x       -   alfa




Mihai Oaida <mihai.oaida@gmail.com>             02
Notiuni avansate MySQL



                                      Tabele
    Tipuri de tabele
      MyISAM
      InnoDB
      Memory / HEAP
      MERGE, MRG_MyISAM
      Black hole
      CSV
      ARCHIVE

    CREATE TABLE t( columns) ENGINE =
    sau
    ALTER TABLE t ENGINE =

Mihai Oaida <mihai.oaida@gmail.com>            03
Notiuni avansate MySQL



                                      Indecsi
    • Ordonam tabelul dupa 1 sau n coloane
    • Tipuri de indecsi
      PRIMARY KEY
      Index
      Unique
      Full Text
    • Tipuri de algoritmi : BTREE si HASH
    • Aplicate pe 1 sau mai multe coloane (multiple index)

    CREATE INDEX i_name ON table_name(col1,col2,..)

Mihai Oaida <mihai.oaida@gmail.com>                     04
Notiuni avansate MySQL



                              Full text index
    Cautare in texte

    SELECT id,titlu FROM table WHERE
    MATCH(titlu) AGAINST(‘xml’)

    SELECT id,titlu FROM table WHERE
    MATCH(titlu) AGAINST(‘xml’ WITH QUERY
      EXPANSION)




Mihai Oaida <mihai.oaida@gmail.com>             05
Notiuni avansate MySQL



                                 Foreign key
    Creeaza legaturile intre tabele care la proiectare
    le creem prin conventie. Ex : news.cat_id – cat.id

    Ex : InnoDB
    Se poate pune la create tabele sau ulterior

    FOREIGN KEY(cat_id) REFERENCES cat(id)




Mihai Oaida <mihai.oaida@gmail.com>                  06
Notiuni avansate MySQL



                                      Triggere
    Obiect asociat unui tabel care se apeleaza la un
     anumit eveniment
    CREATE TRIGGER trig_name {BEFORE| AFTER}
      {INSERT,REPLACE,UPDATE, DELETE} on t
         FOR EACH ROW BEGIN
              ..sql..
    END;




Mihai Oaida <mihai.oaida@gmail.com>                    07
Notiuni avansate MySQL



                                      Exemplu

    CREATE TRIGGER updateCateg AFTER INSERT ON
    produse
     FOR EACH ROW BEGIN
         UPDATE cats SET nr=nr+1 WHERE
      id=NEW.cat_id;
    END;




Mihai Oaida <mihai.oaida@gmail.com>             08
Notiuni avansate MySQL



                         Proceduri stocate
    Subrutine care se retin in baza de date

    Folosite pentru
       A testa datele
       A muta sql din logica aplicatiei in db
       A minimiza traficul dintre client si db




Mihai Oaida <mihai.oaida@gmail.com>              09
Notiuni avansate MySQL



                                      Exemplu
    delimiter //
    DROP PROCEDURE IF EXISTS colavg//
    CREATE PROCEDURE colavg(IN tbl CHAR(64),IN col
      CHAR(64))

    READS SQL DATA
    COMMENT 'Selects the avg of column col in table tbl'
    BEGIN SET @s = CONCAT('SELECT AVG(' , col , ') FROM
       ' , tbl);
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    END;
    //
    delimiter ;

    CALL colavg(‘t_note’,’nota’);

Mihai Oaida <mihai.oaida@gmail.com>                    10
Notiuni avansate MySQL



                                      VIEWS
    CREATE VIEW view_name AS [query]
    DROP VIEW view_name
    CREATE VIEW stats_l AS SELECT l.id,l.nume, ( SELECT
      count( c.id ) FROM comentarii c WHERE c.lectie_id
      = l.id ) AS nr_comentarii , ( SELECT count( r.id
      ) FROM rezolvari r WHERE r.lectie_id = l.id ) AS
      nr_rezolvari FROM lectii l ORDER BY id ASC

    SELECT * FROM stats_l




Mihai Oaida <mihai.oaida@gmail.com>                       11
Notiuni avansate MySQL



                                  DESCRIBE
    • Arata planul de executie ales de optimizator
    • Arata pentru fiecare tabel din sql : indecsi
      folositi,tipul cautarii, nr de randuri prin care
      cauta,etc
    • DESCRIBE [sql]
    • Campuri
         Select_type – tip qurery
         Table – nume tabel
         Possible_keys – key-uri luate in considerare
         Key – cheia aleasa
         Key_len - lungimea
         Rows – numar aprox row-uri prin care cauta
         Extra – file sort, tmp table, etc
Mihai Oaida <mihai.oaida@gmail.com>                      12
Notiuni avansate MySQL



                             Slow query log
    Location my.ini
    log_slow_queries = /var/log/mysql/my-slow.log
    long_query_time = 2
    log-queries-not-using-indexes




Mihai Oaida <mihai.oaida@gmail.com>                 13
Notiuni avansate MySQL



                                Query cache
    Location my.ini
    query_cache_limit                 = 64M
    query_cache_size                  = 64M


    Nu trebuie setata prea mare

    Raportul read/write – invalidarea cache-ului

    Trebuie urmarit cache hits




Mihai Oaida <mihai.oaida@gmail.com>                14
Notiuni avansate MySQL



                                 Performanta
    • Datele retinute pe tipul cel mai mic
         md5 hash – binary(16) pack()
         ip – int 4 bytes ip2long()

    • Indecsi mici – mai multa informatie incape
      intr-un bloc de memorie

    • Indecsi pe cheia de join
    • Binary log off

    Don’t
     SELECT * FROM table ORDER BY RAND() LIMIT 1
     SELECT COUNT(*) FROM table
     SELECT DISTINCT column FROM table
Mihai Oaida <mihai.oaida@gmail.com>                15
Notiuni avansate MySQL



                                      Referinte
    RTFM
      http://dev.mysql.com/doc/refman/5.0/en/
      http://dev.mysql.com/tech-resources/articles/

    Scule
      http://dev.mysql.com/workbench/
      http://dev.mysql.com/downloads/gui-tools/5.0.html
      http://munin.projects.linpro.no/

    Lectura
      http://www.mysqlperformanceblog.com/




Mihai Oaida <mihai.oaida@gmail.com>                       16
Notiuni avansate MySQL




                                Intrebari?




Mihai Oaida <mihai.oaida@gmail.com>

More Related Content

More from Mihai Oaida

Building faster websites Front-end performance
Building faster websites Front-end performanceBuilding faster websites Front-end performance
Building faster websites Front-end performanceMihai Oaida
 
Linux/Unix-based Operating Systems
Linux/Unix-based Operating SystemsLinux/Unix-based Operating Systems
Linux/Unix-based Operating SystemsMihai Oaida
 
Scaling web-applications
Scaling web-applicationsScaling web-applications
Scaling web-applicationsMihai Oaida
 
Arhitecturi de cacheing server side - LVLE 2009
Arhitecturi de cacheing server side - LVLE 2009Arhitecturi de cacheing server side - LVLE 2009
Arhitecturi de cacheing server side - LVLE 2009Mihai Oaida
 
Notiuni avansate MySQL - LVLE 2009
Notiuni avansate MySQL - LVLE 2009Notiuni avansate MySQL - LVLE 2009
Notiuni avansate MySQL - LVLE 2009Mihai Oaida
 
Teme - Proiectarea si normalizarea bazelor de date - Infoeducatie 2008
Teme - Proiectarea si normalizarea bazelor de date - Infoeducatie 2008Teme - Proiectarea si normalizarea bazelor de date - Infoeducatie 2008
Teme - Proiectarea si normalizarea bazelor de date - Infoeducatie 2008Mihai Oaida
 
jQuery - GeekMeet Timisoara
jQuery - GeekMeet TimisoarajQuery - GeekMeet Timisoara
jQuery - GeekMeet TimisoaraMihai Oaida
 
Notiuni avansate MySQL - Infoeducatie 2009
Notiuni avansate MySQL - Infoeducatie 2009Notiuni avansate MySQL - Infoeducatie 2009
Notiuni avansate MySQL - Infoeducatie 2009Mihai Oaida
 
jQuery - Infoeducatie 2008
jQuery - Infoeducatie 2008jQuery - Infoeducatie 2008
jQuery - Infoeducatie 2008Mihai Oaida
 
Arhitecturi de caching server-side - Infoeducatie 2008
Arhitecturi de caching server-side - Infoeducatie 2008 Arhitecturi de caching server-side - Infoeducatie 2008
Arhitecturi de caching server-side - Infoeducatie 2008 Mihai Oaida
 
Proiectarea si normalizarea bazelor de date - Infoeducatie 2008
Proiectarea si normalizarea bazelor de date - Infoeducatie 2008Proiectarea si normalizarea bazelor de date - Infoeducatie 2008
Proiectarea si normalizarea bazelor de date - Infoeducatie 2008Mihai Oaida
 
Arhitectura Si Managementul Proiectelor - Infoeducatie 2007
Arhitectura Si Managementul Proiectelor - Infoeducatie 2007Arhitectura Si Managementul Proiectelor - Infoeducatie 2007
Arhitectura Si Managementul Proiectelor - Infoeducatie 2007Mihai Oaida
 

More from Mihai Oaida (13)

K-Means
K-MeansK-Means
K-Means
 
Building faster websites Front-end performance
Building faster websites Front-end performanceBuilding faster websites Front-end performance
Building faster websites Front-end performance
 
Linux/Unix-based Operating Systems
Linux/Unix-based Operating SystemsLinux/Unix-based Operating Systems
Linux/Unix-based Operating Systems
 
Scaling web-applications
Scaling web-applicationsScaling web-applications
Scaling web-applications
 
Arhitecturi de cacheing server side - LVLE 2009
Arhitecturi de cacheing server side - LVLE 2009Arhitecturi de cacheing server side - LVLE 2009
Arhitecturi de cacheing server side - LVLE 2009
 
Notiuni avansate MySQL - LVLE 2009
Notiuni avansate MySQL - LVLE 2009Notiuni avansate MySQL - LVLE 2009
Notiuni avansate MySQL - LVLE 2009
 
Teme - Proiectarea si normalizarea bazelor de date - Infoeducatie 2008
Teme - Proiectarea si normalizarea bazelor de date - Infoeducatie 2008Teme - Proiectarea si normalizarea bazelor de date - Infoeducatie 2008
Teme - Proiectarea si normalizarea bazelor de date - Infoeducatie 2008
 
jQuery - GeekMeet Timisoara
jQuery - GeekMeet TimisoarajQuery - GeekMeet Timisoara
jQuery - GeekMeet Timisoara
 
Notiuni avansate MySQL - Infoeducatie 2009
Notiuni avansate MySQL - Infoeducatie 2009Notiuni avansate MySQL - Infoeducatie 2009
Notiuni avansate MySQL - Infoeducatie 2009
 
jQuery - Infoeducatie 2008
jQuery - Infoeducatie 2008jQuery - Infoeducatie 2008
jQuery - Infoeducatie 2008
 
Arhitecturi de caching server-side - Infoeducatie 2008
Arhitecturi de caching server-side - Infoeducatie 2008 Arhitecturi de caching server-side - Infoeducatie 2008
Arhitecturi de caching server-side - Infoeducatie 2008
 
Proiectarea si normalizarea bazelor de date - Infoeducatie 2008
Proiectarea si normalizarea bazelor de date - Infoeducatie 2008Proiectarea si normalizarea bazelor de date - Infoeducatie 2008
Proiectarea si normalizarea bazelor de date - Infoeducatie 2008
 
Arhitectura Si Managementul Proiectelor - Infoeducatie 2007
Arhitectura Si Managementul Proiectelor - Infoeducatie 2007Arhitectura Si Managementul Proiectelor - Infoeducatie 2007
Arhitectura Si Managementul Proiectelor - Infoeducatie 2007
 

Advanced Mysql - GeekMeet Timisoara

  • 2. Notiuni avansate MySQL Notiuni avansate MySQL Mysql Tabele Indecsi Foreign key-uri Triggere Proceduri stocate VIEWS DESCRIBE Slow query log Query cache Performance Mihai Oaida <mihai.oaida@gmail.com> 01
  • 3. Notiuni avansate MySQL MySQL 3.23 - vechi 4.1x - stabil, putine feature-uri 5.0x - GA 5.1 - RC 6.0x - alfa Mihai Oaida <mihai.oaida@gmail.com> 02
  • 4. Notiuni avansate MySQL Tabele Tipuri de tabele MyISAM InnoDB Memory / HEAP MERGE, MRG_MyISAM Black hole CSV ARCHIVE CREATE TABLE t( columns) ENGINE = sau ALTER TABLE t ENGINE = Mihai Oaida <mihai.oaida@gmail.com> 03
  • 5. Notiuni avansate MySQL Indecsi • Ordonam tabelul dupa 1 sau n coloane • Tipuri de indecsi PRIMARY KEY Index Unique Full Text • Tipuri de algoritmi : BTREE si HASH • Aplicate pe 1 sau mai multe coloane (multiple index) CREATE INDEX i_name ON table_name(col1,col2,..) Mihai Oaida <mihai.oaida@gmail.com> 04
  • 6. Notiuni avansate MySQL Full text index Cautare in texte SELECT id,titlu FROM table WHERE MATCH(titlu) AGAINST(‘xml’) SELECT id,titlu FROM table WHERE MATCH(titlu) AGAINST(‘xml’ WITH QUERY EXPANSION) Mihai Oaida <mihai.oaida@gmail.com> 05
  • 7. Notiuni avansate MySQL Foreign key Creeaza legaturile intre tabele care la proiectare le creem prin conventie. Ex : news.cat_id – cat.id Ex : InnoDB Se poate pune la create tabele sau ulterior FOREIGN KEY(cat_id) REFERENCES cat(id) Mihai Oaida <mihai.oaida@gmail.com> 06
  • 8. Notiuni avansate MySQL Triggere Obiect asociat unui tabel care se apeleaza la un anumit eveniment CREATE TRIGGER trig_name {BEFORE| AFTER} {INSERT,REPLACE,UPDATE, DELETE} on t FOR EACH ROW BEGIN ..sql.. END; Mihai Oaida <mihai.oaida@gmail.com> 07
  • 9. Notiuni avansate MySQL Exemplu CREATE TRIGGER updateCateg AFTER INSERT ON produse FOR EACH ROW BEGIN UPDATE cats SET nr=nr+1 WHERE id=NEW.cat_id; END; Mihai Oaida <mihai.oaida@gmail.com> 08
  • 10. Notiuni avansate MySQL Proceduri stocate Subrutine care se retin in baza de date Folosite pentru A testa datele A muta sql din logica aplicatiei in db A minimiza traficul dintre client si db Mihai Oaida <mihai.oaida@gmail.com> 09
  • 11. Notiuni avansate MySQL Exemplu delimiter // DROP PROCEDURE IF EXISTS colavg// CREATE PROCEDURE colavg(IN tbl CHAR(64),IN col CHAR(64)) READS SQL DATA COMMENT 'Selects the avg of column col in table tbl' BEGIN SET @s = CONCAT('SELECT AVG(' , col , ') FROM ' , tbl); PREPARE stmt FROM @s; EXECUTE stmt; END; // delimiter ; CALL colavg(‘t_note’,’nota’); Mihai Oaida <mihai.oaida@gmail.com> 10
  • 12. Notiuni avansate MySQL VIEWS CREATE VIEW view_name AS [query] DROP VIEW view_name CREATE VIEW stats_l AS SELECT l.id,l.nume, ( SELECT count( c.id ) FROM comentarii c WHERE c.lectie_id = l.id ) AS nr_comentarii , ( SELECT count( r.id ) FROM rezolvari r WHERE r.lectie_id = l.id ) AS nr_rezolvari FROM lectii l ORDER BY id ASC SELECT * FROM stats_l Mihai Oaida <mihai.oaida@gmail.com> 11
  • 13. Notiuni avansate MySQL DESCRIBE • Arata planul de executie ales de optimizator • Arata pentru fiecare tabel din sql : indecsi folositi,tipul cautarii, nr de randuri prin care cauta,etc • DESCRIBE [sql] • Campuri Select_type – tip qurery Table – nume tabel Possible_keys – key-uri luate in considerare Key – cheia aleasa Key_len - lungimea Rows – numar aprox row-uri prin care cauta Extra – file sort, tmp table, etc Mihai Oaida <mihai.oaida@gmail.com> 12
  • 14. Notiuni avansate MySQL Slow query log Location my.ini log_slow_queries = /var/log/mysql/my-slow.log long_query_time = 2 log-queries-not-using-indexes Mihai Oaida <mihai.oaida@gmail.com> 13
  • 15. Notiuni avansate MySQL Query cache Location my.ini query_cache_limit = 64M query_cache_size = 64M Nu trebuie setata prea mare Raportul read/write – invalidarea cache-ului Trebuie urmarit cache hits Mihai Oaida <mihai.oaida@gmail.com> 14
  • 16. Notiuni avansate MySQL Performanta • Datele retinute pe tipul cel mai mic md5 hash – binary(16) pack() ip – int 4 bytes ip2long() • Indecsi mici – mai multa informatie incape intr-un bloc de memorie • Indecsi pe cheia de join • Binary log off Don’t SELECT * FROM table ORDER BY RAND() LIMIT 1 SELECT COUNT(*) FROM table SELECT DISTINCT column FROM table Mihai Oaida <mihai.oaida@gmail.com> 15
  • 17. Notiuni avansate MySQL Referinte RTFM http://dev.mysql.com/doc/refman/5.0/en/ http://dev.mysql.com/tech-resources/articles/ Scule http://dev.mysql.com/workbench/ http://dev.mysql.com/downloads/gui-tools/5.0.html http://munin.projects.linpro.no/ Lectura http://www.mysqlperformanceblog.com/ Mihai Oaida <mihai.oaida@gmail.com> 16
  • 18. Notiuni avansate MySQL Intrebari? Mihai Oaida <mihai.oaida@gmail.com>