Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    12,547
    Plugin Contributions
    89

    Default MySQL: What does KEY idx_customers_id_zen(customers_id) do?

    I'm in the process of creating a new database table and was looking through the Zen Cart install SQL files for a "good example". I see that many of the tables have not only PRIMARY_KEY definitions, but also KEY definitions like below. What do these extra key definitions do for me? Using the Developers Tool Kit to search for 'idx_customers_id_zen' brings up no results ...

    Code:
    #
    # Table structure for table 'files_uploaded'
    #
    
    DROP TABLE IF EXISTS files_uploaded;
    CREATE TABLE files_uploaded (
      files_uploaded_id int(11) NOT NULL auto_increment,
      sesskey varchar(32) default NULL,
      customers_id int(11) default NULL,
      files_uploaded_name varchar(64) NOT NULL default '',
      PRIMARY KEY  (files_uploaded_id),
      KEY idx_customers_id_zen (customers_id)
    ) ENGINE=MyISAM COMMENT='Must always have either a sesskey or customers_id';

  2. #2
    Join Date
    Feb 2006
    Location
    Tampa Bay, Florida
    Posts
    9,733
    Plugin Contributions
    323

    Default Re: MySQL: What does KEY idx_customers_id_zen(customers_id) do?

    It's just creating a named index.
    http://dev.mysql.com/doc/refman/5.1/...ate-table.html
    That Software Guy. My Store: Zen Cart Support
    Available for hire - See my ad in Services
    Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
    Do you benefit from Zen Cart? Then please support the project.

  3. #3
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    12,547
    Plugin Contributions
    89

    Default Re: MySQL: What does KEY idx_customers_id_zen(customers_id) do?

    Thanks, swguy, I'd read through the MySQL docs on CREATE_TABLE ... but it's a bit Greek to me. I took away that it's a database performance thing, and I'm guessing that it's used internally by MySQL. Other than that, I still have no clue why I would/wouldn't include KEY clauses in my newly-minted table.

  4. #4
    Join Date
    Feb 2006
    Location
    Tampa Bay, Florida
    Posts
    9,733
    Plugin Contributions
    323

    Default Re: MySQL: What does KEY idx_customers_id_zen(customers_id) do?

    Think about the design of your table and the searches that will be performed on it. For instance, look at the address_book table. It has a unique key that's automatically generated. But because lookups are done on customers_id, it also creates an index on that field.
    That Software Guy. My Store: Zen Cart Support
    Available for hire - See my ad in Services
    Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
    Do you benefit from Zen Cart? Then please support the project.

  5. #5
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    12,547
    Plugin Contributions
    89

    Default Re: MySQL: What does KEY idx_customers_id_zen(customers_id) do?

    Thanks for the update, it's a bit more clear to me now.

  6. #6
    Join Date
    Feb 2006
    Location
    Tampa Bay, Florida
    Posts
    9,733
    Plugin Contributions
    323

    Default Re: MySQL: What does KEY idx_customers_id_zen(customers_id) do?

    That Software Guy. My Store: Zen Cart Support
    Available for hire - See my ad in Services
    Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
    Do you benefit from Zen Cart? Then please support the project.

  7. #7
    Join Date
    Jan 2004
    Posts
    66,378
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: MySQL: What does KEY idx_customers_id_zen(customers_id) do?

    PRIMARY KEY is the first and main "index" on the table, and requires (and enforces) uniqueness on the fields listed in its definition (the fields in parentheses)

    KEY is a secondary "index" on other fields or combinations of fields. Can be many indexes (KEY declarations), but should be as few as possible.

    Optimizing them is based on what SELECT statements need to gain most efficient access to the records, primarily built on WHERE or JOIN clause contents. While an ORDER BY clause does impact, the impact of an ORDER BY is very marginal and should not be a primary reason for ever adding an index to any table.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  8. #8
    Join Date
    Sep 2008
    Location
    DownUnder, overlooking South Pole.
    Posts
    976
    Plugin Contributions
    6

    Default Re: MySQL: What does KEY idx_customers_id_zen(customers_id) do?

    Is there a particular naming convention for the idx_whatever_zen part of the KEY definition?

    In KEY idx_customers_id_zen (customers_id), I gather the (customer_id) part refers to the column name, but the reasoning behind idx_customers_id_zen part is not clear to me, as with the following excerpt for the Customers table.

    KEY idx_email_address_zen (customers_email_address),
    KEY idx_referral_zen (customers_referral(10)),
    KEY idx_grp_pricing_zen (customers_group_pricing),
    KEY idx_nick_zen (customers_nick),
    KEY idx_newsletter_zen (customers_newsletter)

    Why the (10) after customers-referral?

    Thanks
    Last edited by dw08gm; 20 Aug 2013 at 08:16 PM.

  9. #9
    Join Date
    Jan 2004
    Posts
    66,378
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: MySQL: What does KEY idx_customers_id_zen(customers_id) do?

    1. the idx_ and _zen prefix/suffix are there to denote that those indexes were (most likely) created by Zen Cart initially, as opposed to those added manually by users themselves (such as via phpMyAdmin which just uses the field name itself).

    2. the names of the fields indexed are generally included in the name of the index, mainly to add clarity. Some abbreviation is occasionally used where logical.

    3. the (10) you mention is an advanced approach to indexing which forces only the first 10 characters of the value to be indexed. This is a requirement for certain field types which can't possibly index the entire field (ie: a longtext field). It shortens the amount of storage required for the indexing, and sometimes even improves speed performance, depending on how the indexes actually get used by the query optimizer engine in mysql
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

 

 

Similar Threads

  1. Replies: 6
    Last Post: 13 Nov 2014, 09:17 PM
  2. MySQL 1062 : Duplicate entry ‘zc_xxxxxxx’ for key 1
    By monkeyboy71 in forum Bug Reports
    Replies: 1
    Last Post: 28 Dec 2009, 12:18 PM
  3. Fix Cache Key ( what the... )
    By Soulkreed in forum General Questions
    Replies: 12
    Last Post: 2 Dec 2009, 01:21 AM
  4. 1034 Incorrect key file for table - after upgrading MySQL
    By macat in forum General Questions
    Replies: 2
    Last Post: 30 Jan 2009, 10:38 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR