इंडेक्स का उपयोग और निर्माण

PostgreSQL में क्वेरी प्रदर्शन सुधारना

Amy McCarty

Instructor

इंडेक्स ओवरव्यू

क्या

  • खोज तेज करने के लिए सॉर्टेड कॉलम keys बनाने की विधि
  • किताब के index जैसा
  • डेटा लोकेशन का संदर्भ

क्यों

  • तेज क्वेरीज़

कहाँ

  • आम फ़िल्टर कॉलम
  • प्राइमरी key
PostgreSQL में क्वेरी प्रदर्शन सुधारना

इंडेक्स उदाहरण

ingredient recipe
tomatoes spaghetti & meatballs
green onions fried rice
eggs fried rice
ground beef spaghetti & meatballs
pasta spaghetti & meatballs
rice fried rice
soy sauce fried rice
SELECT *
FROM cookbook
WHERE recipe = 'fried rice'
PostgreSQL में क्वेरी प्रदर्शन सुधारना

कुंजी और पॉइंटर के रूप में इंडेक्स

दो टेबल। एक Index टेबल में recipe कॉलम और pointers का कॉलम है, जो _12 से _18 तक क्रम में हैं। Index वाला टेबल बाएँ वाले जैसा है, बस एक अतिरिक्त pointer कॉलम के साथ। इस pointer कॉलम के मान Index टेबल के pointer कॉलम जैसे ही हैं। तीर दिखाते हैं कि Index टेबल में fried rice के चार मान Table with Index में fried rice की चार पंक्तियों से कैसे मेल खाते हैं।

PostgreSQL में क्वेरी प्रदर्शन सुधारना

मौजूदा इंडेक्स खोजना

pg_tables
  • information_schema जैसा
    • Postgres के लिए विशेष
  • डेटाबेस का मेटाडेटा
PostgreSQL में क्वेरी प्रदर्शन सुधारना

मौजूदा इंडेक्स खोजना

pg_tables
  • information_schema जैसा
    • Postgres के लिए विशेष
  • डेटाबेस का मेटाडेटा

 

SELECT * FROM pg_indexes
schemaname tablename indexname tablespace indexdef
food dinner recipe_index null CREATE INDEX recipe_index ...
PostgreSQL में क्वेरी प्रदर्शन सुधारना

इंडेक्स बनाना

Dinner टेबल, जिसमें ingredient, recipe, और serving size कॉलम हैं। tomatoes - spaghetti & meatballs - 4. green onions - fried rice - 2. eggs - fried rice - 2. ground beef - spaghetti & meatballs - 4. pasta - spaghetti & meatballs - 4. rice - fried rice - 2. soy sauce - fried rice - 2.

CREATE INDEX recipe_index 
ON cookbook (recipe);
CREATE INDEX CONCURRENTLY recipe_index
ON cookbook (recipe, serving_size);
PostgreSQL में क्वेरी प्रदर्शन सुधारना

उपयोग करें या न करें

इंडेक्स कब उपयोग करें

  • बड़े टेबल
  • आम फ़िल्टर कंडीशन
  • प्राइमरी key

इंडेक्स से बचें

  • छोटे टेबल
  • बहुत null वाले कॉलम
  • बार-बार अपडेट होने वाले टेबल
    • इंडेक्स fragmented होगा
    • डेटा दो जगह लिखा जाएगा
PostgreSQL में क्वेरी प्रदर्शन सुधारना

बार-बार अपडेट होने वाले टेबल

दो टेबल। एक Index टेबल में recipe कॉलम और pointers का कॉलम है, जो _12 से _19 तक क्रम में हैं। Index वाला टेबल बाएँ वाले जैसा है, बस एक अतिरिक्त pointer कॉलम के साथ। इस pointer कॉलम के मान Index टेबल के pointer कॉलम जैसे ही हैं। तीर दिखाते हैं कि Index टेबल में spaghetti and meatballs के चार मान Table with Index में spaghetti and meatballs की चार पंक्तियों से कैसे मेल खाते हैं। तीन मान Index टेबल के ऊपर साथ में सॉर्टेड हैं। एक spaghetti and meatballs नीचे है, जो Table with index में नए basil एंट्री से मेल खाता है।

PostgreSQL में क्वेरी प्रदर्शन सुधारना

इंडेक्स क्वेरी आकलन

क्वेरी प्लानर

एक बड़े पकाने के बर्तन के चारों ओर कई शेफ

EXPLAIN
SELECT * 
FROM cookbook

 

क्वेरी प्लान

Seq scan on cookbook (cost=0.00...22.70
  rows = 1270 width = 36)
  • लागत (समय) अनुमान
PostgreSQL में क्वेरी प्रदर्शन सुधारना

अभ्यास करते हैं!

PostgreSQL में क्वेरी प्रदर्शन सुधारना

Preparing Video For Download...