MySQL
  Home arrow MySQL arrow Page 3 - Optimizing the Logical Database Structure
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
MYSQL

Optimizing the Logical Database Structure
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 14
    2006-08-24


    Table of Contents:
  • Optimizing the Logical Database Structure
  • 13.4.2 Using Summary Tables
  • 13.5 Exercises
  • More Exercises
  • Answers to Exercises
  • More answers

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Optimizing the Logical Database Structure - 13.5 Exercises
    ( Page 3 of 6 )

    Question 1:

    Consider the following table with two indexes:

    mysql> DESCRIBE fastindex;
    +-------+----------+------+-----+
    | Field | Type     | Null | Key |
    +-------+----------+------+-----+
    | i1    | char(10) |      | MUL |
    | i2    | char(10) | YES  | MUL |
    +-------+----------+------+-----+

    With no other facts given, which of the following queries would you expect to run faster?

    SELECT i1 FROM fastindex WHERE i1 LIKE 'mid%';
    
    SELECT i2 FROM fastindex WHERE i2 LIKE 'mid%';

    Question 2:

    Consider the following table with indexes:

    mysql> SHOW CREATE TABLE fastindex;
    +-----------+---------------------------
    | Table     | Create Table
    +-----------+---------------------------
    | fastindex | CREATE TABLE ´fastindex´ (
     ´i1´ char(10) NOT NULL default '',
     ´i2´ char(10) NOT NULL default '',
     KEY ´i1´ (´i1´(3)),
     KEY ´i2´ (´i2´)
    ) TYPE=MyISAM |
    +-----------+---------------------------

    With no other facts given, which of the following queries would you expect to run faster?

    SELECT i1 FROM fastindex WHERE i1 LIKE 'mid%';
    
    SELECT i2 FROM fastindex WHERE i2 LIKE 'mid%';

    Question 3:

    For what reason can adding indexes to a table make table operations slower?

    Question 4:

    Consider the following table structure, which will be used for the next four questions:

    mysql> DESCRIBE City;
    +-------------+----------+------+-----+---------+----------------+
    | Field       | Type     | Null | Key | Default | Extra          |
    +-------------+----------+------+-----+---------+----------------+
    | ID          | int(11)  |      | PRI | NULL    | auto_increment |
    | Name        | char(35) | YES  |     | NULL    |                |
    | CountryCode | char(3)  | YES  |     | NULL    |                |
    | District    | char(20) | YES  |     | NULL    |                |
    | Population  | int(11)  | YES  |     | 0       |                |
    +-------------+----------+------+-----+---------+----------------+

    You frequently retrieve data from the City table, using queries similar to those shown here:

    mysql> SELECT * FROM City WHERE Name BETWEEN 'E'
    AND 'G' ORDER BY Name;
    +------+------------------+-------------+--------------+------------+ | ID | Name | CountryCode | District | Population | +------+------------------+-------------+--------------+------------+ | 735 | East London | ZAF | Eastern Cape | 221047 | | 3963 | East Los Angeles | USA | California | 126379 | | 1845 | East York | CAN | Ontario | 114034 | | 533 | Eastbourne | GBR | England | 90000 | | 1720 | Ebetsu | JPN | Hokkaido | 118805 | | ... | ... | ... | ... | ... | mysql> SELECT * FROM City WHERE CountryCode >= 'Y'
    ORDER BY name;
    +------+------------+-------------+----------------+------------+ | ID | Name | CountryCode | District | Population | +------+------------+-------------+----------------+------------+ | 1781 | Aden | YEM | Aden | 398300 | | 1784 | al-Mukalla | YEM | Hadramawt | 122400 | | 721 | Alberton | ZAF | Gauteng | 410102 | | 724 | Benoni | ZAF | Gauteng | 365467 | | 1792 | Beograd | YUG | Central Serbia | 1204000 | | ... | ... | ... | ... | ... |

    How would you determine the number of rows MySQL must inspect to calculate the result sets?

    Question 5:

    Consider, once again, the table structure and sample queries shown for the City table in the previous question. What index or indexes would you add to the table to speed up the queries?

    Question 6:

    Here again are the table structure and sample queries first shown for the City table two questions previously, but with the addition of the indexes on the Name and CountryCode columns from the previous question:

    mysql> DESCRIBE City;
    +-------------+----------+------+-----+---------+----------------+
    | Field       | Type     | Null | Key | Default | Extra          |
    +-------------+----------+------+-----+---------+----------------+
    | ID          |  int(11) |      | PRI | NULL    | auto_increment |
    | Name        | char(35) | YES  | MUL | NULL    |                |
    | CountryCode |  char(3) | YES  | MUL | NULL    |                |
    | District    | char(20) | YES  |     | NULL    |                |
    | Population  |  int(11) | YES  |     | 0       |                |
    +-------------+----------+------+-----+---------+----------------+
    
    mysql> SELECT * FROM City WHERE Name BETWEEN 'E'
    AND 'G' ORDER BY Name;
    +------+------------------+-------------+--------------+------------+ | ID | Name | CountryCode | District | Population | +------+------------------+-------------+--------------+------------+ | 735 | East London | ZAF | Eastern Cape | 221047 | | 3963 | East Los Angeles | USA | California | 126379 | | 1845 | East York | CAN | Ontario | 114034 | | 533 | Eastbourne | GBR | England | 90000 | | 1720 | Ebetsu | JPN | Hokkaido | 118805 | | ... | ... | ... | ... | ... | mysql> SELECT * FROM City WHERE CountryCode >= 'Y'
    ORDER BY name;
    +------+------------+-------------+----------------+------------+ | ID | Name | CountryCode | District | Population | +------+------------+-------------+----------------+------------+ | 1781 | Aden | YEM | Aden | 398300 | | 1784 | al-Mukalla | YEM | Hadramawt | 122400 | | 721 | Alberton | ZAF | Gauteng | 410102 | | 724 | Benoni | ZAF | Gauteng | 365467 | | 1792 | Beograd | YUG | Central Serbia | 1204000 | | ... | ... | ... | ... | ... |

    In addition to adding indexes to the City table, what else can be done, with regard to the table's columns, to improve performance?

    Question 7:

    Consider, once again, the new table structure and the sample queries shown for the City table in the previous question. How would you find out whether the new indexes on the table are actually used to resolve the queries?

    Question 8:

    Consider the following table:

    mysql> DESCRIBE enumtest;
    +-------+--------------------------------+------+-----+---------+-------+
    | Field | Type                           | Null | Key | Default | Extra |
    +-------+--------------------------------+------+-----+---------+-------+
    | col   | enum('first','second','third') |      | PRI | first   |       |
    +-------+--------------------------------+------+-----+---------+-------+
    mysql> SELECT * FROM enumtest;
    Empty set

    Will the following statement fail or will it insert rows? What will the contents of the enumtest table be after executing the statement?

    mysql> INSERT INTO enumtest VALUES
      -> ('first'),('second'),('third'),('false'),
    ('fourth');

    Question 9:

    Consider the following table, which has two single-column FULLTEXT indexes:

    mysql> DESCRIBE faq;
    +----------+---------------+------+-----+---------+-------+
    | Field    | Type          | Null | Key | Default | Extra |
    +----------+---------------+------+-----+---------+-------+
    | cdate    | timestamp(14) | YES  |     | NULL    |       |
    | question | char(150)     |      | MUL |         |       |
    | answer   | char(250)     |      | MUL |         |       |
    +----------+---------------+------+-----+---------+-------+
    
    mysql> SHOW INDEX FROM faq;
    +-------+------------+----------+-  -+-------------+-  -+------------+-
    | Table | Non_unique | Key_name | ...| Column_name | ...| Index_type | ...
    +-------+------------+----------+-  -+-------------+-  -+------------+-
    | faq   |          1 | question | ...| question    | ...| FULLTEXT   | ...
    | faq   |          1 | answer   | ...| answer      | ...| FULLTEXT   | ...
    +-------+------------+----------+-  -+-------------+-  -+------------+-

    With MATCH ... AGAINST(), you can search the answers and the questions stored in the table. How would you search for a search term 'MySQL' in the question column?

    Question 10:

    Consider the following table, which has two single-column FULLTEXT indexes:

    mysql> DESCRIBE faq;
    +----------+---------------+------+-----+---------+-------+
    | Field    | Type          | Null | Key | Default | Extra |
    +----------+---------------+------+-----+---------+-------+
    | cdate    | timestamp(14) | YES  |     | NULL    |       |
    | question | char(150)     |      | MUL |         |       |
    | answer   | char(250)     |      | MUL |         |       |
    +----------+---------------+------+-----+---------+-------+
    mysql> SHOW INDEX FROM faq;
    +-------+------------+----------+-  -+-------------+-  -+------------+-
    | Table | Non_unique | Key_name | ...| Column_name | ...| Index_type | ...
    +-------+------------+----------+-  -+-------------+-  -+------------+-
    | faq   |          1 | question | ...| question    | ...| FULLTEXT   | ...
    | faq   |          1 | answer   | ...| answer      | ...| FULLTEXT   | ...
    +-------+------------+----------+-  -+-------------+-  -+------------+-

    With MATCH ... AGAINST(), you can search the answers and the questions stored in the table. How would you search for the search term 'Access' in either the question or the answer column?



     
     
    >>> More MySQL Articles          >>> More By Sams Publishing
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek