`
marlonyao
  • 浏览: 249122 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Indexes and Query Speed (from hsqldb document)

阅读更多

HSQLDB does not use indexes to improve sorting of query results. But indexes have a crucial role in improving query speed. If no index is used in a query on a single table, such as a DELETE query, then all the rows of the table must be examined. With an index on one of the columns that is in the WHERE clause, it is often possible to start directly from the first candidate row and reduce the number of rows that are examined.

Indexes are even more important in joins between multiple tables. SELECT ... FROM t1 JOIN t2 ON t1.c1 = t2.c2 is performed by taking rows of t1 one by one and finding a matching row in t2. If there is no index index on t2.c2 then for each row of t1, all the rows of t2 must be checked. Whereas with an index, a matching row can be found in a fraction of the time. If the query also has a condition on t1, e.g., SELECT ... FROM t1 JOIN t2 ON t1.c1 = t2.c2 WHERE t1.c3 = 4 then an index on t1.c3 would eliminate the need for checking all the rows of t1 one by one, and will reduce query time to less than a millisecond per returned row. So if t1 and t2 each contain 10,000 rows, the query without indexes involves checking 100,000,000 row combinations. With an index on t2.c2, this is reduced to 10,000 row checks and index lookups. With the additional index on t2.c2, only about 4 rows are checked to get the first result row.

Indexes are automatically created for primary key and unique columns. Otherwise you should define an index using the CREATE INDEX command.

Note that in HSQLDB a unique index on multiple columns can be used internally as a non-unique index on the first column in the list. For example: CONSTRAINT name1 UNIQUE (c1, c2, c3); means there is the equivalent of CREATE INDEX name2 ON atable(c1);. So you do not need to specify an extra index if you require one on the first column of the list.

In 1.8.0, a multi-column index will speed up queries that contain joins or values on ALL the columns. You need NOT declare additional individual indexes on those columns unless you use queries that search only on a subset of the columns. For example, rows of a table that has a PRIMARY KEY or UNIQUE constraint on three columns or simply an ordinary index on those columns can be found efficiently when values for all three columns are specified in the WHERE clause. For example, SELECT ... FROM t1 WHERE t1.c1 = 4 AND t1.c2 = 6 AND t1.c3 = 8 will use an index on t1(c1,c2,c3) if it exists.

As a result of the improvements to multiple key indexes, the order of declared columns of the index or constraint has less affect on the speed of searches than before. If the column that contains more diverse values appears first, the searches will be slightly faster.

A multi-column index will not speed up queries on the second or third column only. The first column must be specified in the JOIN .. ON or WHERE conditions.

Query speed depends a lot on the order of the tables in the JOIN .. ON or FROM clauses. For example the second query below should be faster with large tables (provided there is an index on TB.COL3). The reason is that TB.COL3 can be evaluated very quickly if it applies to the first table (and there is an index on TB.COL3):

    (TB is a very large table with only a few rows where TB.COL3 = 4)

    SELECT * FROM TA JOIN TB ON TA.COL1 = TB.COL2 AND TB.COL3 = 4;

    SELECT * FROM TB JOIN TA ON TA.COL1 = TB.COL2 AND TB.COL3 = 4;

The general rule is to put first the table that has a narrowing condition on one of its columns.

1.7.3 features automatic, on-the-fly indexes for views and subselects that are used in a query. An index is added to a view when it is joined to a table or another view.
Where Condition or Join

Using WHERE conditions to join tables is likely to reduce execution speed. For example the following query will generally be slow, even with indexes:

    SELECT ... FROM TA, TB, TC WHERE TC.COL3 = TA.COL1 AND TC.COL3=TB.COL2 AND TC.COL4 = 1

The query implies TA.COL1 = TB.COL2 but does not explicitly set this condition. If TA and TB each contain 100 rows, 10000 combinations will be joined with TC to apply the column conditions, even though there may be indexes on the joined columns. With the JOIN keyword, the TA.COL1 = TB.COL2 condition has to be explicit and will narrow down the combination of TA and TB rows before they are joined with TC, resulting in much faster execution with larger tables:

    SELECT ... FROM TA JOIN TB ON TA.COL1 = TB.COL2 JOIN TC ON TB.COL2 = TC.COL3 WHERE TC.COL4 = 1

The query can be speeded up a lot more if the order of tables in joins are changed, so that TC.COL1 = 1 is applied first and a smaller set of rows are joined together:

    SELECT ... FROM TC JOIN TB ON TC.COL3 = TB.COL2 JOIN TA ON TC.COL3 = TA.COL1 WHERE TC.COL4 = 1

In the above example the engine automatically applies TC.COL4 = 1 to TC and joins only the set of rows that satisfy this condition with other tables. Indexes on TC.COL4, TB.COL2 and TA.COL1 will be used if present and will speed up the query.
Subqueries and Joins

Using joins and setting up the order of tables for maximum performance applies to all areas. For example, the second query below should generally be much faster if there are indexes on TA.COL1 and TB.COL3:

Example 2.2. Query comparison

    SELECT ... FROM TA WHERE TA.COL1 = (SELECT MAX(TB.COL2) FROM TB WHERE TB.COL3 = 4)

    SELECT ... FROM (SELECT MAX(TB.COL2) C1 FROM TB WHERE TB.COL3 = 4) T2 JOIN TA ON TA.COL1 = T2.C1

The second query turns MAX(TB.COL2) into a single row table then joins it with TA. With an index on TA.COL1, this will be very fast. The first query will test each row in TA and evaluate MAX(TB.COL2) again and again.

分享到:
评论

相关推荐

    8.3 Optimization and Indexes

    mysql5.6官方文档:8.3 Optimization and Indexes

    Expert.Oracle.Indexing.and.Access.Paths.2nd.epub

    Maintain indexes so as to provide consistent and predictable query response over the lifetime of an application. Expert Oracle Indexing and Access Paths is about the one database structure at the ...

    Expert.Oracle.Indexing.and.Access.Paths

    Maintain indexes so as to provide consistent and predictable query response over the lifetime of an application. Expert Oracle Indexing and Access Paths is about the one database structure at the ...

    Beginning PHP and MySQL: From Novice to Professional, 5th Edition

    Get started with PHP and MySQL programming: no experience necessary. This fifth edition of a classic ...Chapter 33: Indexes and Searching Chapter 34: Transactions Chapter 35: Importing and Exporting Data

    Study on Human Resource Niche Concept and Evaluation Indexes

    人力资源生态位概念与评价指标研究,颜爱民,刘媛,人力资源管理正经历着从“基于工作”向“基于能力”的重大思想转变。在高动态的知识经济背景下,“基于能力”的人力资源管理思想

    SQL Query Intellisense and Database Design Tool BDB 2.7.0.3

    SQL Query Intellisense and Database Design Tool for Oracle, SQLServer, MySQL, Access, SQLAnywhere and Sybase. · Database design Build the database model by BDB, support forward-engineering and ...

    Managing Gigabytes: Compressing and Indexing Documents and Images

    "The coverage of compression, file organizations, and indexing techniques for full text and document management systems is unsurpassed. Students, researchers, and practitioners will all benefit from ...

    微软内部资料-SQL性能优化5

    Because the trees are balanced, finding any record requires about the same amount of resources, and retrieval speed is consistent because the index has the same depth throughout. Clustered and ...

    Expert Performance Indexing in SQL Server(Apress,2ed,2015)

    This book is a deep dive into perhaps the single-most important facet of good performance: indexes, and how to best use them. The book begins in the shallow waters with explanations of the types of ...

    《SQL数据库管理工具》(Richardson Software RazorSQL)v5.1.1/含注册机

    * Visual tools for creating, editing, dropping, describing, altering, and viewing tables, views, indexes, and sequences * Visual Tools for creating, editing, dropping, and executing stored procedures,...

    Pro Couchbase Server(Apress,2ed,2015)

    It also brings coverage of multiple new features, including the new generation of client SDKs, security and LDAP integration, secondary indexes, and multi-dimensional scaling. Pro Couchbase Server ...

    Pro.SQL.Server.Internals

    The book provides a solid road map for understanding the depth and power of the SQL Server database server and teaches how to get the most from the platform and keep your databases running at the ...

    MongoDB The Definitive Guide

    Use indexes, aggregation tools, and other advanced query techniques Learn about monitoring, security and authentication, backup and repair, and more Set up master-slave and automatic failover ...

    Beginning PHP and MySQL

    Beginning PHP and MySQL: From Novice to Professional, Fourth Edition is a major update of W. Jason Gilmore’s authoritative book on PHP and MySQL. The fourth edition includes complete coverage of ...

    Pro SQL Server Internals

    including In-Memory OLTP, columnstore enhancements, Operational Analytics support, Query Store, JSON, temporal tables, stretch databases, security features, and other improvements in the new SQL ...

    Analysis on the Relationship Between Photosynthetic Indexes and Drought Resistance in Sugarcane (2014年)

    利用因子分析方法研究了甘蔗叶片净光合速率(Pn)、胞间CO2浓度(Ci)、气孔导度(Gs)、蒸腾速率(Tr)、水分利用率(WUE)、PSⅡ原初光能转换效率(Fv/Fm)、PSⅡ潜在活性(Fv/Fo)等光合参数与抗旱性的关系。...

    Hibernate Search In Action

    results into managed objects of your domain model, and eases the transition from a HQL-based query to a full-text query. Hibernate Search also helps you scale Lucene in a clustered environment. ...

Global site tag (gtag.js) - Google Analytics