sql index改怎么建
https://stackoverflow.com/questions/11299217/how-can-i-optimize-this-sql-query-using-indexes
---------------------------------------------------------------------------------------------------
Intro: There is a lot to talk about here, and because of the complexity of SQL, it's going to be impossible for anyone to help with your query fully – it matters what your Query is, how large the tables are, and what the database system being used is. If you don’t know what indexes are, or how to use them, see here: How does database indexing work?.
Precaution: Again, if you have a DBA for your system, check with them before indexing anything, especially on a live system. They can even help, if you're nice to them. If the system is used by many others, be careful before changing anything like indexes. If the data is being used for multiple query types, make sure you aren't creating tons of indexes on them that conflict or overlap.
Syntax. The standard (SQL92) uses: CREATE INDEX [index name] ON [table name] ( [column name] ). This syntax should work on almost any system. If you need only one index on the table, and there is not already a clustered index, you can use: CREATE [Unique] Clustered INDEX [index name] ON [table name] ( [column name] ) - it should be unique if there cannot be multiple items with the same values. If you can't get this to work, see this post for more details: How do I index a database column.
Which tables should be indexed? Any table that is being used for querying, especially if the data is static or only gets new values, is a great candidate. If the table is in your query, and has a join statement, you probably want to have an index on the column(s) being joined.
What columns should be indexed? There are full books written on choosing the best indexes, and how to properly index a database. A basic rule of thumb for indexing, if you don't want to dive deep into the problem, is: index by the following, in this order:
- Join predicates (
on Table1.columnA=Table2.ColumnA and Table1.columnB=Table2.ColumnQ) - Filtered columns (
where Table1.columnN=’Bob’ and Table1.columnS<20) - Order by / Group By / etc. (any column which is used for the order/grouping should be in the index, if possible.)
Also:
- Use data types that make sense - store nothing as varchar if it's an integer or date. (Column width matters. Use the smallest data type you can, if possible.)
- Make sure your joins are the same data type - int to int, varchar to varchar, and so on.
- If possible, use unique, non-null indexes on each join predicate in each tables.
Make sure whatever columns possible are non-null. (If they cannot contain null values, you can use the following syntax.
Alter table Table1
alter column columnN int not null
Do all of this, and you'll be well on your way. But if you need this stuff regularly, learn it! Buy a book, read online, find the information. There is a lot of information out there, and it is a deep topic, but you can make queries MUCH better if you know what you are doing.
sql index改怎么建的更多相关文章
- SQL 数据优化索引建suo避免全表扫描
首先什么是全表扫描和索引扫描?全表扫描所有数据过一遍才能显示数据结果,索引扫描就是索引,只需要扫描一部分数据就可以得到结果.如果数据没建立索引. 无索引的情况下搜索数据的速度和占用内存就会比用索引的检 ...
- SQL SERVER 生成MYSQL建表脚本
/****** Object: StoredProcedure [dbo].[GET_TableScript_MYSQL] Script Date: 06/15/2012 13:05:14 ***** ...
- SQL SERVER 生成ORACLE建表脚本
/****** Object: StoredProcedure [dbo].[GET_TableScript_ORACLE] Script Date: 06/15/2012 13:07:16 **** ...
- ORACLE基本SQL语句-用户及建表篇
一.用户相关SQL语句 /*新建用户*/create user ; 说明:SA用户名,2013密码 /*授权connect,resource给用户sa*/grant connect,resource ...
- 将一个多表关联的条件查询中的多表通过 create select 转化成一张单表的sql、改为会话级别临时表 【我】
将一个多表关联的条件查询中的多表通过 create select 转化成一张单表的sql 将结果改为创建一个会话级别的临时表: -- 根据下面这两个sql CREATE TABLE revenu ...
- j接近50道经典SQL练习题,附建表SQL解题SQL
说明 本文章整理了47道常见sql联系题,包括建表语句,表结构,习题列表,解题答案都涵盖在本文章内.文末提供了所用SQL脚本下载链接.所有解题答案都是本人自己写的,广大读者如果在阅读使用中,有任何问题 ...
- 50个SQL语句(MySQL版) 建表 插入数据
本学期正在学习数据库,前段时间老师让我们做一下50个经典SQL语句,当时做的比较快,有一些也是百度的,自我感觉理解的不是很透彻. 所以从本篇随笔开始,我将进行50个经典SQL语句的复盘,加深理解. 答 ...
- SQL语句一之建库
USE master --转到系统表goIF EXISTS(SELECT * FROM sysdatabases WHERE name ='Test') --查询是否存在Test数据库DROP DA ...
- mssql sql server上如何建一个只读视图–视图锁定的另类解决方案
转自:http://www.maomao365.com/?p=4508 <span style="color:red;font-weight:bold;">我们熟知一个 ...
随机推荐
- Java学习,从入门到放弃(一)SpringMVC+Maven+Mybits 多种数据库配置(mysql+sqlserver)AOP方式
多数据库配置需求有两种,一种是因为项目太大,访问量太高,不得不分布多个数据库减轻访问压力,比较多的应用就是读写分离:另一种就是原本不同的两个数据库业务现在要整合到一起,甚至连数据库都不一样,一个mys ...
- IOPS 测试工具 FIO
FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎. fio-2.8下载: wget http://brick.kernel.dk/snaps/fio-2.8 ...
- 捕捉AVPlayerViewController 系统原生工具栏的出现、隐藏事件
需求前提 1. app内轻量级的视频播放功能,故不希望引入“过度开发.过度封装”的第三方控件组,使用原生的AVPlayerViewController 2. 工具栏有新增控件需求,如下载按钮 等 3. ...
- myeclipse 相关问题
[如何设置 Tab 键为四个空格] https://blog.csdn.net/QQ826688096/article/details/90543252
- Jenkins+maven+gitlab自动化部署之Jenkins系统管理配置(四)
一.Jenkins全局工具配置 在jenkins首页依次进入系统管理>>全局工具配置: 1) jdk.git.maven配置 指定其在服务器中的目录位置 二.插件管理 1)依次点开系统管理 ...
- 计算机网络自顶向下方法第4章 网络层:数据平面 (Network layer)
4.1 网络层概述 网络层主要功能为转发(将数据从路由器输入接口转移到合适的输出接口)和路由选择(端到端的路径选择),每台路由器都有一张转发表,用最长前缀匹配规则来转发. 4.1.1 转发和路由选择 ...
- PAT(B) 1062 最简分数(Java)
题目链接:1062 最简分数 (20 point(s)) 题目描述 一个分数一般写成两个整数相除的形式:N/M,其中 M 不为0.最简分数是指分子和分母没有公约数的分数表示形式. 现给定两个不相等的正 ...
- 【字符串hash】DNA
DNA 题目描述 小X身为奆老,兴趣爱好广泛,他还非常喜欢研究DNA序列……小X进行了一项关于DNA序列研究,发现人某条染色体上的一段DNA序列中连续的k个碱基组成的碱基序列与做题的AC率有关!于是他 ...
- javascript之instanceof
定义和用法 instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上. 语法: object instanceof construct ...
- SSH和SSM对比异同点、各自优势
1SSH和SSM定义SSH 通常指的是 Struts2 做控制器(Action),Spring 管理各层的组件,Hibernate 负责持久化层. SSM 则指的是 SpringMVC 做控制器(co ...