SlideShare a Scribd company logo
1 of 15
Download to read offline
Optimizacija MySql-a
 ● Osnove
 ● Vrste tabel

 ● Benchmarking

 ● Index-i

 ● Učinkovitost queryev (poizvedb)
Osnove MySql-a


➲   Zakaj MySql?
    ●   Enostavna namestitev
    ●   Enostavna uporaba
➲   Replikacija > zanesljivo, eden od serverjev
    sigurno dela
    ●   Tudi zaradi performance
➲   my.cnf > default, za velik sistem bm
➲   Benchmarking > v cvs oz. backup



                          2                razvoj@3zan.si
Vrste tabel




    3         razvoj@3zan.si
Vrste tabel


➲   MyISAM
    ●   1. file: .MYD - podatkovni file (privzeta velikost
        4 GB)
    ●   2. file: .MYI – index file (32 biten kazalec do
        zapisov v bazi)
    ●   Je mogoče, da se ne da brati ali pisati iz tabele?
        Odgovor: Da. Pride do bad sectorja, zmanjka
        elektrike.
        ●   Ponovna indeksacija tabele



                               4                    razvoj@3zan.si
Benchmarking


➲   Igra “kaj če”
➲   Vse lahko vemo pa se vseeno lahko izkaže,
    da ni res.
➲   Bistvo je spreminjanje in testiranje
    spremenjenega.
➲   Vsekakor mora biti del vsakega
    posodabljanja oz. dodajanja novosti.
➲   Testiramo tudi na realnih podatkih.
    ●   Bolha: Na localhostu imam 50 zapisov, na
        Bolhi pa recimo 200 – krat toliko.
                          5                        razvoj@3zan.si
Primeri - benchmarking


➲   Ena ponovitev določenega query-ja dela
    dobro. Kaj pri 100- ali 1000- kratnih
    ponovitvah?
➲   Ali bo dodajanje RAM-a res povečalo hitrost
    query-ja za 2- ali 3- krat?
➲   Ima res smisel uporabiti ORDER BY v
    query-ju? Odgovor: Ne, če ni nujno in se
    čas izvedbe query-ja poveča za 100 -krat
➲   Kaj je bolje: uporaba subquery-ja ali dveh
    ločenih query-jev?
                      6                   razvoj@3zan.si
Index-i 1/4


➲   Včasih delajo čudeže, včasih pa samo
    upočasnjujejo insert stavke in porabljajo
    prostor na disku
➲   Kaj sploh je index?
   Stvarno kazalo ☺
    ●
   Tabela izbranih polj in pripadajočih kazalcev na
    ●
   zapise teh izbranih polj
 ● Primer: Telefonski imenik
➲ Naredimo index tistega polja po katerem
  največkrat iščemo
➲ Hitrost: DESC, ASC
                       7                      razvoj@3zan.si
Index-i 2/4


➲   Ne smemo pretiravati z dodajanjem
    index-ov:
   Zaradi prevelike porabe prostora na disku
    ●
   Upočasni se izvajanje INSERT, UPDATE in
    ●
   DELETE stavkov
 ● V skrajnih primerih bi potrebovali že tabelo
   index-ov tabele indexov☺
➲ Delna index-acija polj
 ● Za zelo velike baze, da prihranimo na prostoru
 ● Primer: priimek(4)


                      8                      razvoj@3zan.si
Index-i 3/4


➲   Index-acija več polj
    ●   Če imamo v WHERE-u pogosto dve polji skupaj
    ●   Primer: Bolha (datum, active)
    ●   Kljub temu se poizveduje najprej po prvem in
        potem po drugem
    ●   Zakaj ne naredimo index-a na vsako polje
        posebej?
➲   Unique
    ●   Uporabljamo jih, kadar smo prepričani, da so
        vrednosti nekega polja pri vseh zapisih različni
    ●   Primer: telefonska številka
                           9                       razvoj@3zan.si
Index – i 4/4


●   Kaj delajo?
    ●   Funkcije index-ov
    ●   Pri INSERT, UPDATE stavkih preveri, če je še kakšen
        isti zapis
●   Vprašanje, ki se poraja: Kaj je razlika med
    unique-om in primary key-om?




                          10                        razvoj@3zan.si
Izboljšanje performance query-jev
                 1/3

➲   Prej ali slej pridemo do počasnih query-jev
    (benchmarking)
➲   MySQL ima že optimizer
    ●   Poskuša dobiti odgovore:
        ●   Izbira indeksa s katerim bi najhitreje našli ustrezne
            zapise
        ●   Če sodeluje več tabel, izbere kateri indeks je najboljši
        ●   Ugotovi optimalen “join order”
    ●   Ker dela brez testiranja, se moti
    ●   Zato EXPLAIN (ANALYZE TABLE – posodobi
        index statistics)
        ●   Primeri
                                11                           razvoj@3zan.si
Izboljšanje performance query-jev
                 2/3

➲   JOIN-i
    ●   Optimizer se sam odloča kateri join bo najprej
        bral
    ●   Ni pameten, saj uporablja brute-force
    ●   Poskusi vse kombinacije preden se odloči za
        eno (10:1 za testiranje sek.)
➲   Optimizer
    ●   Če imamo ogromno raznovrstno tabelo, moramo
        itak pregledati 50% tabele; MySQL ne uporablja
        index-ov
    ●   Privzet “cutoff” je 30%
                          12                      razvoj@3zan.si
Izboljšanje performance query-jev
                 3/3

    ●   Če potrebujemo podatke samo iz tabele index-
        ov, je pameten
    ●   Pri sortiranju upošteva dva index-a skupaj;
        vzame najbolje od obeh (pazljivo)
    ●   Pameten z nemogočimi query-ji
➲   Vrstni red JOIN-ov
    ●   Ponavadi je vseeno (STRAIGHT_JOIN)
➲   Triki
    ●   Dva query-ja je bolje kot eden
    ●   UNION namesto OR
    ●   LEFT_JOIN
                          13                    razvoj@3zan.si
Replikacija


➲   Primer: Bolha
➲   Kaj s tem rešimo?
    ●   Porazdelitev podatkov
    ●   Load balancing (Bolha)
    ●   Varnostna kopija (backup) / vzpostavitev stanja
        po “sesutju” (recovery)
    ●   Popolna razpoložljivost podatkov
➲   Slabosti
    ●   Delay pri sinhronizaciji
    ●   Slave mora delati isto kot master

                           14                     razvoj@3zan.si
Povezave

➲   http://video.google.com/videoplay?docid=2524524540025172110&q=g
    oogle+engedu
➲   http://www.java2s.com/Code/SQL/CatalogSQL.htm




                             15                            razvoj@3zan.si

More Related Content

Featured

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

MySQL optimization

  • 1. Optimizacija MySql-a ● Osnove ● Vrste tabel ● Benchmarking ● Index-i ● Učinkovitost queryev (poizvedb)
  • 2. Osnove MySql-a ➲ Zakaj MySql? ● Enostavna namestitev ● Enostavna uporaba ➲ Replikacija > zanesljivo, eden od serverjev sigurno dela ● Tudi zaradi performance ➲ my.cnf > default, za velik sistem bm ➲ Benchmarking > v cvs oz. backup 2 razvoj@3zan.si
  • 3. Vrste tabel 3 razvoj@3zan.si
  • 4. Vrste tabel ➲ MyISAM ● 1. file: .MYD - podatkovni file (privzeta velikost 4 GB) ● 2. file: .MYI – index file (32 biten kazalec do zapisov v bazi) ● Je mogoče, da se ne da brati ali pisati iz tabele? Odgovor: Da. Pride do bad sectorja, zmanjka elektrike. ● Ponovna indeksacija tabele 4 razvoj@3zan.si
  • 5. Benchmarking ➲ Igra “kaj če” ➲ Vse lahko vemo pa se vseeno lahko izkaže, da ni res. ➲ Bistvo je spreminjanje in testiranje spremenjenega. ➲ Vsekakor mora biti del vsakega posodabljanja oz. dodajanja novosti. ➲ Testiramo tudi na realnih podatkih. ● Bolha: Na localhostu imam 50 zapisov, na Bolhi pa recimo 200 – krat toliko. 5 razvoj@3zan.si
  • 6. Primeri - benchmarking ➲ Ena ponovitev določenega query-ja dela dobro. Kaj pri 100- ali 1000- kratnih ponovitvah? ➲ Ali bo dodajanje RAM-a res povečalo hitrost query-ja za 2- ali 3- krat? ➲ Ima res smisel uporabiti ORDER BY v query-ju? Odgovor: Ne, če ni nujno in se čas izvedbe query-ja poveča za 100 -krat ➲ Kaj je bolje: uporaba subquery-ja ali dveh ločenih query-jev? 6 razvoj@3zan.si
  • 7. Index-i 1/4 ➲ Včasih delajo čudeže, včasih pa samo upočasnjujejo insert stavke in porabljajo prostor na disku ➲ Kaj sploh je index? Stvarno kazalo ☺ ● Tabela izbranih polj in pripadajočih kazalcev na ● zapise teh izbranih polj ● Primer: Telefonski imenik ➲ Naredimo index tistega polja po katerem največkrat iščemo ➲ Hitrost: DESC, ASC 7 razvoj@3zan.si
  • 8. Index-i 2/4 ➲ Ne smemo pretiravati z dodajanjem index-ov: Zaradi prevelike porabe prostora na disku ● Upočasni se izvajanje INSERT, UPDATE in ● DELETE stavkov ● V skrajnih primerih bi potrebovali že tabelo index-ov tabele indexov☺ ➲ Delna index-acija polj ● Za zelo velike baze, da prihranimo na prostoru ● Primer: priimek(4) 8 razvoj@3zan.si
  • 9. Index-i 3/4 ➲ Index-acija več polj ● Če imamo v WHERE-u pogosto dve polji skupaj ● Primer: Bolha (datum, active) ● Kljub temu se poizveduje najprej po prvem in potem po drugem ● Zakaj ne naredimo index-a na vsako polje posebej? ➲ Unique ● Uporabljamo jih, kadar smo prepričani, da so vrednosti nekega polja pri vseh zapisih različni ● Primer: telefonska številka 9 razvoj@3zan.si
  • 10. Index – i 4/4 ● Kaj delajo? ● Funkcije index-ov ● Pri INSERT, UPDATE stavkih preveri, če je še kakšen isti zapis ● Vprašanje, ki se poraja: Kaj je razlika med unique-om in primary key-om? 10 razvoj@3zan.si
  • 11. Izboljšanje performance query-jev 1/3 ➲ Prej ali slej pridemo do počasnih query-jev (benchmarking) ➲ MySQL ima že optimizer ● Poskuša dobiti odgovore: ● Izbira indeksa s katerim bi najhitreje našli ustrezne zapise ● Če sodeluje več tabel, izbere kateri indeks je najboljši ● Ugotovi optimalen “join order” ● Ker dela brez testiranja, se moti ● Zato EXPLAIN (ANALYZE TABLE – posodobi index statistics) ● Primeri 11 razvoj@3zan.si
  • 12. Izboljšanje performance query-jev 2/3 ➲ JOIN-i ● Optimizer se sam odloča kateri join bo najprej bral ● Ni pameten, saj uporablja brute-force ● Poskusi vse kombinacije preden se odloči za eno (10:1 za testiranje sek.) ➲ Optimizer ● Če imamo ogromno raznovrstno tabelo, moramo itak pregledati 50% tabele; MySQL ne uporablja index-ov ● Privzet “cutoff” je 30% 12 razvoj@3zan.si
  • 13. Izboljšanje performance query-jev 3/3 ● Če potrebujemo podatke samo iz tabele index- ov, je pameten ● Pri sortiranju upošteva dva index-a skupaj; vzame najbolje od obeh (pazljivo) ● Pameten z nemogočimi query-ji ➲ Vrstni red JOIN-ov ● Ponavadi je vseeno (STRAIGHT_JOIN) ➲ Triki ● Dva query-ja je bolje kot eden ● UNION namesto OR ● LEFT_JOIN 13 razvoj@3zan.si
  • 14. Replikacija ➲ Primer: Bolha ➲ Kaj s tem rešimo? ● Porazdelitev podatkov ● Load balancing (Bolha) ● Varnostna kopija (backup) / vzpostavitev stanja po “sesutju” (recovery) ● Popolna razpoložljivost podatkov ➲ Slabosti ● Delay pri sinhronizaciji ● Slave mora delati isto kot master 14 razvoj@3zan.si
  • 15. Povezave ➲ http://video.google.com/videoplay?docid=2524524540025172110&q=g oogle+engedu ➲ http://www.java2s.com/Code/SQL/CatalogSQL.htm 15 razvoj@3zan.si