SQL 基础
SQL 简介
SQL 入门
SQL 语法
SQL 创建数据库
SQL 创建表
SQL Constraints
SQL Insert
SQL Select
SQL Where
SQL AND & OR
SQL IN & Between
SQL Order By
SQL Top/Limit
SQL Distinct
SQL Update
SQL Delete
SQL Truncate Table
SQL Drop
SQL 连接
SQL Joining Tables
SQL Inner Join
SQL Left Join
SQL Right Join
SQL Full Join
SQL Cross Join
高级 SQL
SQL Union
SQL Like
SQL Alter Table
SQL Aliases
SQL Group By
SQL Having
SQL Create View
SQL Create Index
SQL Dates and Times
SQL Cloning Tables
SQL 临时表
SQL 子查询
SQL 注入
SQL 参考
SQL 数据类型
MySQL 数据类型
SQL Server 数据类型
SQL 方法
SQL Create Index - SQL基础教程 - 笔下光年
网站首页
SQL Create Index
In this tutorial you will learn how to create indexes on tables to improve the database performance. ## What is Index? An index is a data structure associated with a table that provides fast access to rows in a table based on the values in one or more columns (the index key). Let's say, you have a customers table in your database and you want to find out all the customers whose names begin with the letter A, using the following statement. ```sql SELECT cust_id, cust_name, address FROM customers WHERE cust_name LIKE 'A%'; ``` To find such customers, server must scan each row one by one in the customers table and inspect the contents of the name column. While it works fine for a table having few rows, but imagine how long it might take to answer the query if the table contains million of rows. In such situation you can speed things up by applying indexes to the table. ## Creating an Index You can create indexes with the CREATE INDEX statement: ```sql CREATE INDEX index_name ON table_name (column_name); ``` For example, to create an index on the name column in the customers table, you could use: ```sql CREATE INDEX cust_name_idx ON customers (cust_name); ``` By default, the index will allow duplicate entries and sort the entries in ascending order. To require unique index entries, add the keyword UNIQUE after CREATE, like this: ```sql CREATE UNIQUE INDEX cust_name_idx ON customers (cust_name); ``` In MySQL you can look at the available indexes on a specific table, like this: ```sql mysql> SHOW INDEXES FROM customers \G ``` <div class="callout callout-success mb-3">Tip: Terminate a SQL statement with \G instead of ; to display the result vertically rather than normal tabular format if they are too wide for the current window.</div> ## Creating Multi-column Indexes You can also build the indexes that span multiple columns. For example, suppose you've a table in your database named users having the columns first_name and last_name, and you frequently access the user's records using these columns then you can build an index on both the columns together to improve the performance, as follow: ```sql CREATE INDEX user_name_idx ON users (first_name, last_name); ``` <div class="callout callout-success mb-3">Tip: You can consider a database index as index section of a book that helps you quickly find or locate a specific topic within the book.</div> ## The Downside of Indexes Index should be created with care. Because, every time a row is added, updated or removed from a table, all indexes on that table must be modified. Therefore, the more indexes you have, the more work the server needs to do, which finally leads to slower performance. Here are some basic guidelines that you can follow while creating index: - Index columns that you frequently use to retrieve the data. - Don't create indexes for columns that you never use as retrieval keys. - Index columns that are used for joins to improve join performance. - Avoid columns that contain too many NULL values. Also, small tables do not require indexes, because in the case of small tables, it is usually faster for the server to scan the table rather than look at the index first. <div class="callout callout-info mb-3">Note: Most database system like MySQL, SQL server, etc. automatically creates the indexes for PRIMARY KEY and UNIQUE columns, when the table was created.</div> ## Drop Indexes You can drop indexes that are no longer required with the following statement. ```sql DROP INDEX index_name ON table_name; ``` The following statement will drop the index cust_name_idx from the customers table. ```sql DROP INDEX cust_name_idx ON customers; ``` Moreover, if you drop a table then all associated indexes are also dropped. <div class="callout callout-danger mb-3">Warning: You should investigate thoroughly before dropping an index. As a general rule of thumb, never create or drop indexes blindly.</div>
上一篇:
SQL Create View
下一篇:
SQL Dates and Times