参考文档:http://www.datastax.com/documentation/cql/3.0/webhelp/index.html#cql/ddl/ddl_primary_index_c.html#concept_ds_vk2_dyz_zj

索引提供了一种手段通过属性来获取 Cassandra中数据而不是分区键。好处是提供了快速的、高效的按照指定条件找出数据的查询。

列的值的索引在一个与值分开的、隐藏的表中。Cassandra有很多技术用来防止出现不良的情况——数据可能检索不正确,查询的数据是旧的版本。

一、什么时候使用索引

Cassandra's built-in indexes are best on a table having many rows that contain the indexed value. The more unique values that exist in a particular column, the more overhead you will have, on average, to query and maintain the index. For example, suppose you had a playlists table with a billion songs and wanted to look up songs by the artist. Many songs will share the same column value for artist. The artist column is a good candidate for an index.

When not to use an index

Do not use an index in these situations:

•On high-cardinality columns because you then query a huge volume of records for a small number of results . . . more

•In tables that use a counter column

•On a frequently updated or deleted column . . . more

•To look for a row in a large partition unless narrowly queried . . . more

Problems using a high-cardinality column index

If you create an index on a high-cardinality column, which has many distinct values, a query between the fields will incur many seeks for very few results. In the table with a billion songs, looking up songs by writer (a value that is typically unique for each song) instead of by their artist, is likely to be very inefficient. It would probably be more efficient to manually maintain the table as a form of an index instead of using the Cassandra built-in index. For columns containing unique data, it is sometimes fine performance-wise to use an index for convenience, as long as the query volume to the table having an indexed column is moderate and not under constant load.

Conversely, creating an index on an extremely low-cardinality column, such as a boolean column, does not make sense. Each value in the index becomes a single row in the index, resulting in a huge row for all the false values, for example. Indexing a multitude of indexed columns having foo = true and foo = false is not useful.

Problems using an index on a frequently updated or deleted column

Cassandra stores tombstones in the index until the tombstone limit reaches 100K cells. After exceeding the tombstone limit, the query that uses the indexed value will fail.

Problems using an index to look for a row in a large partition unless narrowly queried

A query on an indexed column in a large cluster typically requires collating responses from multiple data partitions. The query response slows down as more machines are added to the cluster. You can avoid a performance hit when looking for a row in a large partition by narrowing the search, as shown in the next section.

About indexing

An index in Cassandra refers to an index on column values. Cassandra implements an index as a hidden table, separate from the table that contains the values being indexed. Using CQL, you can create an index on a column after defining a table. The music service example shows how to create an index on the artists column of playlist, and then querying Cassandra for songs by a particular artist::

CREATE INDEX artist_names ON playlists( artist );

An index name is optional. If you provide an index name, such as artist_idx, the name must be unique within the keyspace. After creating an index for the artist column and inserting values into the playlists table, greater efficiency is achieved when you query Cassandra directly for artist by name, such as Fu Manchu:

SELECT * FROM playlists WHERE artist = 'Fu Manchu';

As mentioned earlier, when looking for a row in a large partition, narrow the search. This query, although a contrived example using so little data, narrows the search to a single id.

SELECT * FROM playlists WHERE id = 62c36092-82a1-3a00-93d1-46196ee77204 AND artist = 'Fu Manchu';

The output is:

Using multiple indexes

For example purposes, let's say you can create multiple indexes, for example on album and title columns of the playlists table, and use multiple conditions in the WHERE clause to filter the results. In a real-world situation, these columns might not be good choices, depending on their cardinality, as described later:

CREATE INDEX album_name ON playlists ( album );

CREATE INDEX title_name ON playlists ( title );

SELECT * FROM playlists

WHERE album = 'Roll Away' AND title = 'Outside Woman Blues'

ALLOW FILTERING ;

When multiple occurrances of data match a condition in a WHERE clause, Cassandra selects the least-frequent occurrence of a condition for processing first for efficiency. For example, suppose data for Blind Joe Reynolds and Cream's versions of "Outside Woman Blues" were inserted into the playlists table. Cassandra queries on the album name first if there are fewer albums named Roll Away than there are songs called "Outside Woman Blues" in the database. When you attempt a potentially expensive query, such as searching a range of rows, Cassandra requires the ALLOW FILTERING directive.

Building and maintaining indexes

An advantage of indexes is the operational ease of populating and maintaining the index. Indexes are built in the background automatically, without blocking reads or writes. Client-maintained tables as indexes must be created manually; for example, if the state column had been indexed by creating a table such as users_by_state, your client application would have to populate the table with data from the users table.

Cassandra1.2文档学习(19)—— CQL索引的更多相关文章

  1. Cassandra1.2文档学习解读计划——为自己鼓劲

    最近想深入研究一下Cassandra,而Cassandra没有中文文档,仅有的一些参考书都是0.7/0.6版本的.因此有个计划,一边学习文档(地址:http://www.datastax.com/do ...

  2. Cassandra1.2文档学习(17)—— CQL数据模型(上)

    参考文档:http://www.datastax.com/documentation/cql/3.0/webhelp/index.html#cql/ddl/ddl_anatomy_table_c.ht ...

  3. Cassandra1.2文档学习(18)—— CQL数据模型(下)

    三.集合列 CQL 3 引入了一下集合类型: •set •list •map 在关系型数据库中,允许用户拥有多个email地址,你可以创建一个email_addresses表与users表存在一个多对 ...

  4. Cassandra1.2文档学习(15)—— 配置数据一致性

    参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/dml/dml_config ...

  5. Cassandra1.2文档学习(13)—— 数据读取

    参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/dml/dml_about_ ...

  6. Cassandra1.2文档学习(4)——分区器

    参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/a ...

  7. Cassandra1.2文档学习(1)——Cassandra基本说明

    参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/a ...

  8. Cassandra1.2文档学习(16)—— 模式的变化

    参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/dml/dml_schema ...

  9. Cassandra1.2文档学习(9)—— 数据写入

    数据参考:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/dml/manage_dml ...

随机推荐

  1. VS VC++ 设置版本号

    我并不是专职的VC++的开发者,只是有时候偶尔要开发一些C++的DLL,每当要发布新版本的时候,隔得时间长一点总会忘记了在那里设置生成文件的版本号. 在这里把VC++设置的步骤记录下来,以备忘! 设置 ...

  2. Asp.Net 之 通过调用 WScript.Shell 启动本地 exe 程序时产生“ automation服务器不能创建对象 ”的错误

    我们经常需要通过生成 ActiveXObject("WScript.Shell"); 来调用某一exe文件. 设置网页打印的页眉页脚为空: var HKEY_Root,HKEY_P ...

  3. Eclipse开发JQuery环境设置(Spket)

     http://www.cnblogs.com/sayo/archive/2008/10/20/1315528.html   首先需要安装Spket.可以参阅我的这篇文章进行Spket的安装. 之后进 ...

  4. C. Guess Your Way Out!

    C. Guess Your Way Out!                                                               time limit per ...

  5. Recommended you 3 most popular Nissan pincode calculators

    Have you still felt confused on how to choose a satisfactory Nissan pin code calculator in the marke ...

  6. 获取地理位置的html5代码

    /** * 以下为html5代码,获取地理位置 */ function getLocation() { //检查浏览器是否支持地理位置获取 if (navigator.geolocation) { / ...

  7. Eclipse+Axis使用WSDL文件生成Web Service服务端/客户端

    JDK版本:1.5.0_22 Eclipse版本:Helios Service Release 2(3.6.2) WSDL文件的创建过程见http://blog.csdn.net/a19881029/ ...

  8. 用bash命令得到Windows一个目录下的所有文件并且把结果输入到一个文件

    方式一: 只用如下一条语句就可以了: tree/f>index.txt 放入一个文件中命名为"****.bat" 双击就会在该目录下生成一个index.txt文件,在这个文件 ...

  9. Scala中的数组

    数组 数组的两种声明方式,建议声明数组时指定类型. 访问数组元素时获取数组下标 数组Array类本身有很多非常方便的方法 变长数组ArrayBuffer,能够动态增加元素,也可以实现与Array的互转 ...

  10. 【基本计数方法---加法原理和乘法原理】UVa 11538 - Chess Queen

    题目链接 题意:给出m行n列的棋盘,当两皇后在同行同列或同对角线上时可以互相攻击,问共有多少种攻击方式. 分析:首先可以利用加法原理分情况讨论:①两皇后在同一行:②两皇后在同一列:③两皇后在同一对角线 ...