zoukankan      html  css  js  c++  java
  • 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:

    1. Join predicates (on Table1.columnA=Table2.ColumnA and Table1.columnB=Table2.ColumnQ)
    2. Filtered columns (where Table1.columnN=’Bob’ and Table1.columnS<20)
    3. 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.

  • 相关阅读:
    【原创】【js】screenLeft screenTop screenX screenY属性的有效性和兼容性研究
    兼容兼容兼容:浏览器兼容性大集合
    Vue 打包成APP后首屏出现白屏问题
    uniapp 安卓app端本地打包错误: Not found -1,6 at view.umd.min.js:1
    uniapp 下获取cid
    uniapp fill abort statuscode:-1
    Vuex刷新时数据会消失,那如何解决?为什么还要使用Vuex
    vue 安装失败;vue不是内部或外部命令
    js 文件下载,多个文件下载,pdf下载
    uni-App 去掉顶部导航栏
  • 原文地址:https://www.cnblogs.com/oxspirt/p/11382769.html
Copyright © 2011-2022 走看看