Differences between INDEX, PRIMARY, UNIQUE, FULLTEXT in MySQL?

来源:http://www.cnblogs.com/kungfupanda/archive/2016/10/11/5947944.html
-Advertisement-
Play Games

487down vote Differences KEY or INDEX refers to a normal non-unique index. Non-distinct values for the index are allowed, so the index may contain row ...


487down vote

Differences

  • KEY or INDEX refers to a normal non-unique index.  Non-distinct values for the index are allowed, so the index may contain rows with identical values in all columns of the index.  These indexes don't enforce any restraints on your data so they are used only for making sure certain queries can run quickly.

  • UNIQUE refers to an index where all rows of the index must be unique.  That is, the same row may not have identical non-NULL values for all columns in this index as another row.  As well as being used to speed up queries, UNIQUE indexes can be used to enforce restraints on data, because the database system does not allow this distinct values rule to be broken when inserting or updating data.

    Your database system may allow a UNIQUE index to be applied to columns which allow NULL values, in which case two rows are allowed to be identical if they both contain a NULL value (the rationale here is that NULL is considered not equal to itself).  Depending on your application, however, you may find this undesirable: if you wish to prevent this, you should disallow NULL values in the relevant columns.

  • PRIMARY acts exactly like a UNIQUE index, except that it is always named 'PRIMARY', and there may be only one on a table (and there should always be one; though some database systems don't enforce this).  A PRIMARY index is intended as a primary means to uniquely identify any row in the table, so unlike UNIQUE it should not be used on any columns which allow NULL values.  Your PRIMARY index should be on the smallest number of columns that are sufficient to uniquely identify a row.  Often, this is just one column containing a unique auto-incremented number, but if there is anything else that can uniquely identify a row, such as "countrycode" in a list of countries, you can use that instead.

    Some database systems (such as MySQL's InnoDB) will store a table's records on disk in the order in which they appear in the PRIMARY index.

  • FULLTEXT indexes are different from all of the above, and their behaviour differs significantly between database systems.  FULLTEXT indexes are only useful for full text searches done with the MATCH() / AGAINST() clause, unlike the above three - which are typically implemented internally using b-trees (allowing for selecting, sorting or ranges starting from left most column) or hash tables (allowing for selection starting from left most column).

    Where the other index types are general-purpose, a FULLTEXT index is specialised, in that it serves a narrow purpose: it's only used for a "full text search" feature.

Similarities

  • All of these indexes may have more than one column in them.

  • With the exception of FULLTEXT, the column order is significant: for the index to be useful in a query, the query must use columns from the index starting from the left - it can't use just the second, third or fourth part of an index, unless it is also using the previous columns in the index to match static values.  (For a FULLTEXT index to be useful to a query, the query must use all columns of the index.)

share|improve this answer edited Jan 29 '13 at 0:17                 answered Apr 2 '09 at 6:22          thomasrutter        67.2k15105138        
 
2                                                                                  
does this mean a FULLTEXT index is essentially useless and a waist of space if you do not use MATCH() / AGAINST() in your queries?                     – user1397417                 May 1 '14 at 5:07                                                                            
4                                                                                  
Yes. It's also only used for MyISAM databases on MySQL, not InnoDB.  Other database servers may have equivalent features that may work differently.                     – thomasrutter                 May 1 '14 at 6:50                                                                            
                                                                                                                    
"it should not be used on any columns which allow NULL values" --> This should be "cannot be used".  Primary keys are necessarily NOT NULL.  MySQL will report in show columns that a non-NULL unique key is a primary key, if there are no other primary keys defined.                     – Gordon Linoff                 Sep 6 '14 at 20:44                                                                            
                                                                                                                    
The rationale here is that NULL is considered not equal to itself .. Lol i will not forget this                     – Hos Mercury                 Aug 8 at 6:42                                                                            
add a comment |                      
 
No problem. We won't show you that ad again. Why didn't you like it?
Oops! I didn't mean to do this.

         up vote100down vote

All of these are kinds of indices.

primary: must be unique, is an index, is (likely) the physical index, can be only one per table.

unique: as it says. You can't have more than one row with a tuple of this value. Note that since a unique key can be over more than one column, this doesn't necessarily mean that each individual column in the index is unique, but that each combination of values across these columns is unique.

index: if it's not primary or unique, it doesn't constrain values inserted into the table, but it does allow them to be looked up more efficiently.

fulltext: a more specialized form of indexing that allows full text search. Think of it as (essentially) creating an "index" for each "word" in the specified column.

share|improve this answer edited Sep 2 '13 at 16:53          user         2,32852446                 answered Apr 2 '09 at 0:45            tpdi         23.6k853105        
 
22                                                                                  
Primarys can be composite, i.e. multi-key, in MySQL (and many other DBs). They're just a special index. Unique isn't really an index, it's a constraint (which does require an index to enforce in reasonable amounts of time, thus creates one).                     – MBCook                 Apr 2 '09 at 0:48                                                                            
add a comment |                      

         up vote3down vote

I feel like this has been well covered, maybe except for the following:

  • Simple KEY / INDEX (or otherwise called SECONDARY INDEX) do increase performance if selectivity is sufficient. On this matter, the usual recommendation is that if the amount of records in the result set on which an index is applied exceeds 20% of the total amount of records of the parent table, then the index will be ineffective. In practice each architecture will differ but, the idea is still correct.

  • Secondary Indexes (and that is very specific to mysql) should not be seen as completely separate and different objects from the primary key. In fact, both should be used jointly and, once this information known, provide an additional tool to the mysql DBA: in Mysql, indexes embed the primary key. It leads to significant performance improvements, specifically when cleverly building implicit covering indexes such as described there

  • If you feel like your data should be UNIQUE, use a unique index. You may think it's optional (for instance, working it out at application level) and that a normal index will do, but it actually represents a guarantee for Mysql that each row is unique, which incidentally provides a performance benefit.

  • You can only use FULLTEXT (or otherwise called SEARCH INDEX) with Innodb (In MySQL 5.6.4 and up) and Myisam Engines

  • You can only use FULLTEXT on CHAR, VARCHAR and TEXT column types
  • FULLTEXT index involves a LOT more than just creating an index. There's a bunch of system tables created, a completely separate caching system and some specific rules and optimizations applied. See http://dev.mysql.com/doc/refman/5.7/en/fulltext-restrictions.html and http://dev.mysql.com/doc/refman/5.7/en/innodb-fulltext-index.html

 

http://stackoverflow.com/questions/707874/differences-between-index-primary-unique-fulltext-in-mysql


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本文主要介紹9patch圖 *.9.png:android手機上,可以按需求自動拉伸的圖片 本文地址:http://www.cnblogs.com/wuyudong/p/5947195.html,轉載請註明出處。 使用AS點開XXX.9.png圖片 可以看到圖片的四條邊有黑色的線段。下麵通過示意圖來 ...
  • 鏡像下載地址:https://developers.google.com/android/nexus/images 1、打開手機 設置-關於手機-點擊版本號7次,以打開“開發者選項” 2、返回上一步,開發者選項-打開USB 調試3、將手機關機4、同時按住電源鍵&音量減鍵,進入bootloader界面 ...
  • 本文來實現《Android 手機衛士--導航界面1的佈局編寫》中的圖片選擇器部分的代碼。 本文地址:http://www.cnblogs.com/wuyudong/p/5944356.html,轉載請註明出處。 這個可以參考官網提供的API文檔 圖片選擇器編寫 在選中和未選中的過程中,切換展示圖片 ...
  • 1.生命周期 關於生命周期,在詳細講解下: 上圖是從android官網獲取的生命周期。 正常的流程,很多文章都討論過了,我們討論幾個特殊的情況。 1)OnResume->OnPause->OnResume 我們看這段解釋:The activity is no longer visible。 也就是說 ...
  • 這個代理傳值是經常使用的一種傳值方式,下麵介紹一種View 和 Controller 之間的代理傳值方法。 先建立一個View視圖 如 LoginView 是繼承於一個UIView 在LoginView.h裡面聲明協議 LoginView.h文件 import @class LoginView; / ...
  • 背景: 雖然Azure sql database有DMVs可以查看DTU等使用情況,但記錄有時間限制,不會一直保留。為了更好監控Azure_sql_database上各個庫的DTU使用情況、資料庫磁碟使用情況、阻塞等情況。通過本地的Agent的job使用link server 鏈接到各個Azure ...
  • SQL 基礎知識梳理(四) - 數據更新 【博主】反骨仔 【原文】http://www.cnblogs.com/liqingwen/p/5929786.html 序 這是《SQL 基礎知識梳理(三) - 聚合和排序》的下篇。 目錄 插入數據 刪除數據 更新數據 事務 一、插入數據 1.INSERT ...
  • 本設計基於以下需求提出 1. 快速接入數據源表(貼源/落地) 2. 無須給單獨表開發轉換/作業 3. 動態生成數據源連接, 表欄位等信息(預先保存在數據倉庫中) 本設計所需條件 1. 數據源為關係型資料庫 2. 不同數據源需要寫一小段Java Scripts以保證數據源連接可用 總體作業結構 jb_ ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...