Advanced Mysql - GeekMeet Timisoara

963 views

Published on

O prezentare a notiunilor avansate adaugate in Mysql 5.0

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
963
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Advanced Mysql - GeekMeet Timisoara

  1. 1. Notiuni avansate Mysql
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 18. Notiuni avansate MySQL Intrebari? Mihai Oaida <mihai.oaida@gmail.com>

×