Scaler Topics LogoScaler Topics Logo
Search for Articles, Topics

How Index Works in SQL?

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Start Learning View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

Overview

Index is a table that helps in faster query results and quick data retrieval from the database. Index tables use indexing, a technique that uses data structures to optimize the searching time of a database query. Indexing makes database performance better. It also consumes lesser space in the main memory.

What is an INDEX?

Index usually consists of two columns which are a key-value pair. The two columns of the index table (i.e., the key-value pair) contain copies of selected columns of the tabular data of the database.

Data Refrence

Generally, we store the selected Primary or Candidate keys in a sorted manner so that we can reduce the overall query time or search time (from linear search to binary search).

Data Reference contains a set of pointers that holds the address of the disk block. The pointed disk block contains the actual data referred to by the Search Key. Data Reference is also called Block Pointer because it uses block-based addressing.

Indexing is used to quickly retrieve particular data from the database. Indexing reduces the number of disks required to access a particular data by internally creating an index table. Indexing is achieved by creating Index-table or Index.

How to create An Index?

The index can be created very easily using SQL commands.

Syntax:

Suppose we have a database of students. The student table has 4 4 columns: student ID, Name, Physics (marks in Physics), and Chemistry (marks in Chemistry).

Student table:

IDNamePhysicsChemistry
1Aman8692
2Sushant9191
3Saumya9898
4Aditya9388
5Nilesh8783

Let us create an index on the student table using the ID column.

Let us learn some of the advantages of using indexing on the tables of the database.

Advantages of Creating Index Table or Indexing:

  • Indexing helps in faster query results or quick data retrieval.
  • Indexing helps in faster sorting and grouping of records
  • Some Indexing uses sorted and unique keys, which helps to retrieve sorted queries even faster.
  • Index tables are smaller in size, so they require lesser memory.
  • As Index tables are smaller in size, they are stored in the main memory.
  • Since CPU speed and secondary memory speed have a large difference, the CPU uses this main memory index table to bridge the gap of speeds.
  • Indexing helps in better CPU utilization and better performance.

Refer to the image below to see how index works in SQL and how indexing divides the large database into smaller sub-contents.

root node

How does Index Work?

Now that we know index and its advantages, let us now know how index works in SQL. Generally, the database takes the column provided with the CREATE INDEX command and sorts the column values using a special data structure called B-trees.

B-tree is a type of tree data structure that contains 2 things: Index Key and its corresponding disk address. Index Key refers to a certain disk address, and that disk further contains rows or tuples of data. Using B-tree, we can achieve fast searches and fast retrievals. Also, the disk space used is low.

Since the data structure, B_tree, is sorted in order, it makes our search more efficient. So, this is how the index works in SQL. Now let us take a few examples to visualize how index works in SQL.

Example - We want to store the below three-column table in the database.

NameMarksAge
Jone528
Alex3245
Tom3723
Ron8713
Mark2048
Bob8932

The B-tree representation will be like this:

key pair

In the index table, the left column contains the index key, which holds the reference to a particular table row.

Let us take an example to understand how the index works in SQL. Suppose we have the database of a college which stores the details of students. We will store the name, age, CGPA, roll number, and section of students.

The student table is shown below:

Student table

Roll_NoCGPANameSection
19.21AmanA
28.86AdityaA
139.92SushantC
249.71Mohit-
58.56SaumyaC

Suppose the index of the above table is roll_index. Now, a search using the index field can increase performance. The index uses B-tree, so this allows SQL for a binary search of the index with an average of log(n) accesses.

:::{.highlight} Note: We should use indexing when the table contains numerous entries. For a smaller table, the time taken by indexing is somewhat similar to a query without an index table.

:::

How Does the Database Know What Other Fields in the Table to Return?

As we have discussed above, the index table has two columns. The first column stores the key that points (or references) to a particular location of information. Using the student table example, we can visualize that a certain roll number index will point to a record of the table. The record can contain numerous columns or attributes. Hence, using an index, the database can know about the rest of the attributes of the table.

Conclusion

  • Index is a table that helps in faster query results and quick data retrieval from the database. Index tables use indexing, a technique that uses data structures to optimize the searching time of a database query.
  • Indexing makes better database performance. It also consumes lesser space in the main memory.
  • The database takes the column provided with the CREATE INDEX command and sorts the column values using a special data structure B-trees.
  • Index tables are smaller in size, so they require lesser memory. As Index tables are smaller in size, they are stored in the main memory.
  • Since CPU speed and secondary memory speed have a large difference, the CPU uses this main memory index table to bridge the gap of speeds.
  • Indexing helps in better CPU utilization and better performance.

Read More:

PHP网站源码荷坳seo网站推广南山seo网站优化同乐至尊标王深圳外贸网站设计南山网络广告推广宝安网站seo优化爱联高端网站设计坪山百度竞价东莞外贸网站设计盐田seo网站推广永湖百度爱采购光明网站优化按天扣费深圳网页制作深圳如何制作网站罗湖网站关键词优化惠州企业网站设计沙井百搜词包大运百度网站优化排名坑梓建设网站深圳百度关键词包年推广东莞百搜标王广州网站建设设计丹竹头网站改版爱联关键词排名塘坑百姓网标王荷坳模板网站建设平湖网站改版石岩设计网站光明网站关键词优化平湖SEO按天扣费歼20紧急升空逼退外机英媒称团队夜以继日筹划王妃复出草木蔓发 春山在望成都发生巨响 当地回应60岁老人炒菠菜未焯水致肾病恶化男子涉嫌走私被判11年却一天牢没坐劳斯莱斯右转逼停直行车网传落水者说“没让你救”系谣言广东通报13岁男孩性侵女童不予立案贵州小伙回应在美国卖三蹦子火了淀粉肠小王子日销售额涨超10倍有个姐真把千机伞做出来了近3万元金手镯仅含足金十克呼北高速交通事故已致14人死亡杨洋拄拐现身医院国产伟哥去年销售近13亿男子给前妻转账 现任妻子起诉要回新基金只募集到26元还是员工自购男孩疑遭霸凌 家长讨说法被踢出群充个话费竟沦为间接洗钱工具新的一天从800个哈欠开始单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#中国投资客涌入日本东京买房两大学生合买彩票中奖一人不认账新加坡主帅:唯一目标击败中国队月嫂回应掌掴婴儿是在赶虫子19岁小伙救下5人后溺亡 多方发声清明节放假3天调休1天张家界的山上“长”满了韩国人?开封王婆为何火了主播靠辱骂母亲走红被批捕封号代拍被何赛飞拿着魔杖追着打阿根廷将发行1万与2万面值的纸币库克现身上海为江西彩礼“减负”的“试婚人”因自嘲式简历走红的教授更新简介殡仪馆花卉高于市场价3倍还重复用网友称在豆瓣酱里吃出老鼠头315晚会后胖东来又人满为患了网友建议重庆地铁不准乘客携带菜筐特朗普谈“凯特王妃P图照”罗斯否认插足凯特王妃婚姻青海通报栏杆断裂小学生跌落住进ICU恒大被罚41.75亿到底怎么缴湖南一县政协主席疑涉刑案被控制茶百道就改标签日期致歉王树国3次鞠躬告别西交大师生张立群任西安交通大学校长杨倩无缘巴黎奥运

PHP网站源码 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化