I'm trying to create a partitioned index using the below query in Couchbase
CREATE INDEX adv_firstOpen ON data(state, name, zip, status)
WHERE type = 'Event' and name = 'firstOpen'
WITH {"num_partition":4,
"nodes":["node1:9001", "node2:9001", "node3:9001","node4:9001" ]}
but creating replication index
what is the issue in creating an index query?
After reading the Couchbase document got an idea to create a proper index
CREATE INDEX adv_firstOpen ON data(state, name, zip, status)
PARTITION BY HASH(state)
WHERE type = 'Event' and name = 'firstOpen' WITH {"num_partition":4}
here num_partition is enough in with condition and PARTITION BY HASH must be mention
Related
Our server gets slow, "To get data from mysql database". So I search for it on google. They told me, "Use INDEX for the select query to get data from the database it becomes more fastest execution".
Index is a small copy of a database table sorted by key values.
U need to create index first.
CREATE INDEX index_name ON table_name(column_name)
Then:
SELECT * FROM table_name
USE INDEX (index_name)
WHERE condition;
I am working with Couchbase 6.0. I know that we can create Secondary index on a filter.
So I have created a index like
CREATE INDEX idx_zipcode
ON userbucket(zipcode)
WHERE status = "active";
I have a question here:
Can I create an index on a filter clause if field is dynamic.
Something like this
CREATE INDEX idx_zipcode
ON userbucket(zipcode)
WHERE status = ? ;
Second question is,
which one is better in terms of performance:
Single index on 2 fields
CREATE INDEX idx_1 ON userbucket('fname','lname')
or
Separate Index on each field
CREATE INDEX idx_1 ON userbucket('fname')
CREATE INDEX idx_2 ON userbucket('lname')
No we can not create the index with the dynamic clause mentioned accepting a bind variable.
However, when it is sure that the status and zipcode are part of predicates and dynamic in nature the index like below would be handy.
CREATE INDEX idx_zipcode_status ON userbucket(zipcode, status);
Refer Couchbase Index Creation Blog - right performance.
Regarding the second query the same principle applies as
Index selection for a query solely depends on the filters in the WHERE
clause of your query
Secondary Composite index is also okay when you have both or leading columns in your query.
CREATE INDEX idx_1 ON userbucket('fname','lname')
The above index would be exploited by queries like:
SELECT * FROM userbucket WHERE fname= 'fnam' AND lname= 'lnam';
My Table Schema is
CREATE TABLE ITEMS(Time , Name, Token) PRIMARY_KEY(Time, NAME).
Where Time is the timestamp the item is created. When i do the following query
SELECT Name, Token from ITEMS where name = shoes
it takes a while to load the data as my table has more than million rows.
Should i need to add INDEX for faster retrieval of data? I already have an INDEX for this table as there is a PRIMARY KEY.
You need a separate index for name. The primary key index can handle name, but only in conjunction with time.
If you defined it instead as:
PRIMARY_KEY(Name, Time)
Then your query could take advantage of the index.
MySQL has pretty good documentation on composite indexes here.
When you create index using PRIMARY_KEY(Time, NAME), these values will be concatenated. There is no way for MySQL to use this index to search by NAME.
BTW, you may get lot of useful hints from query optimiser if you use EXPLAN keyword in front of your query like this:
EXPLAIN SELECT Name, Token from ITEMS where name = shoes
Keep your eye on output marked "where". This tells how many records MySQL needs to fetch and examine manually after all indexes are exhausted. No need to wait or test in blind.
I have a MySQL DB with two columns. 'Key' and 'Used'. Key is a string, Used is an integer. Is there a very fast way to search for a specific Key and then return the Use in a huge MySQL DB with 6000000 rows of data.
You can make it fast by creating an index on key field:
CREATE INDEX mytable_key_idx ON mytable (`key`);
You can actually make it even faster for reading by creating covering index on both (key, used) fields:
CREATE INDEX mytable_key_used_idx ON mytable (`key`, `used`);
In this case, when reading, MySQL could retrieve used value from the index itself, without reading the table (index-only scan). However, if you have a lot of write activity, covering index may work slower because now it has to update both an index and actual table.
The normative SQL for that would be:
SELECT t.key, t.used FROM mytable t WHERE t.key = 'particularvalue' ;
The output from
EXPLAIN
SELECT t.key, t.used FROM mytable t WHERE t.key = 'particularvalue' ;
Would give details about the access plan, what indexes are being considered, etc.
The output from a
SHOW CREATE TABLE mytable ;
would give information about the table, the engine being used and the available indexes, as well as the datatypes.
Slow performance on a query like this is usually indicative of a suboptimal access plan, either because suitable indexes are not available, or not being used. Sometimes, a characterset mismatch between the column datatype and the literal datatype in the predicate can make an index "unusable" by a particular query.
I have a large database containing more than five million records, this database has three fields (ID, name, text), the field ID has a primary key, the field name has a FULLTEXT index.
I want to create a search engine for my site that seeks in the field name, I use FULLTEXT index but has the disadvantage not to accept the keywords of less than four characters, so I decided to delete it and put a INDEX KEY on the field name and use the following request:
EXPLAIN SELECT * FROM table WHERE locate ('search', name) > 0;
the problem is that this application does not use the index KEY field name,
but this request:
EXPLAIN SELECT name FROM table WHERE locate ('search', name) > 0;
uses the INDEX KEY,
I do not know why when I select all fields MYSQL does not use index.
In your opinion how to solve this problem and if possible a better alternative.
You can set the minimum amount of characters for full text indexes in the mysql configuration. I am not at my computer at the moment to find a example however this page might help you: http://dev.mysql.com/doc/refman/5.1/en/fulltext-fine-tuning.html
Update:
Back at my pc. So regarding why mysql would use a index on the SELECT name FROM table WHERE locate ('search', name) > 0; statement is very simple. When you create a index on the name field the index contains the actual name field i.e. the value of the name field, so when you select only the name field mysql can do a search and retrieve all the data required from the index. So in this scenario mysql has to do one operation to retrieve the data which match the searched values in the index and return them.
The SELECT name FROM table WHERE locate ('search', name) > 0; however needs the other data fields as well. Since only the name field's value is stored in the index mysql will have to read the index and then the table to retrieve the other fields. So in this scenario mysql has to match the values in the index then find the values on the table and then return them. This means mysql has to do 2 operations which is double the amount of work compared to the previous scenario.
Since 5 million rows is still very small it is probably faster for mysql to just loop through the table and retrieve the rows. As you add more rows mysql will probably start using the index once the cost of looping through the table is higher than the cost of reading the index and then looking up the values on the table.
Hope that makes sense.