I have a table with spatial column(data type geometry) and with around 450k rows. When i tried to add a spatial index on this column, it returns an error as "All parts of a SPATIAL index must be NOT NULL".
The query to create index is
create spatial index spatIdx on table_name(ogc_geom)
1. Am I doing something wrong?
2. Where these NULL parts came from?
3. If its in my spat data how can i remove it (i tried with is null).
In the MySQL documentation, it states, "Currently, columns in spatial indexes must be declared NOT NULL". My guess is the column ogc_geom is allowed to have NULL. Try:
ALTER TABLE table_name MODIFY COLUMN ogc_geom .... NOT NULL
Any column you create a spatial index on must be defined with "NOT NULL", or else you will get an error.
how about if you use the "ALTER TABLE" statement instead to update your table structure and add the index into it.
try to check the sysntax from this link: http://dev.mysql.com/doc/refman/5.5/en/alter-table.html
Related
Currently I have a MYSQL query that's under performing. I need to create an index on two fields within my query: cancel and complete.
What is the best way to perform this?
SELECT* FROM table WHERE cancel != 'CANCEL' AND complete IN ('COMPLETE','TAKEN_BACK')
To create a multi-column index, but the column names in parentheses.
ALTER TABLE table
ADD INDEX (cancel, complete);
This is equivalent to
CREATE INDEX table_cancel_complete_ix
ON table (cancel, complete);
CREATE INDEX requires you to give a name to the index, it's optional with ALTER TABLE (it will be assigned a default name).
To add an index over more than one column (aka multi-column index or composite index), use this syntax:
ALTER TABLE your_table
ADD INDEX (cancel, complete);
However, you might want to make sure this is the reason for your statement to be underperforming. Take a look into the manual to dig deeper into this.
We are having problems with a couchbase N1QL Query.
We have an index defined as follows:
CREATE INDEX `AppUser_SubjectId3` ON `Portal`(`SubjectId`) WHERE ((meta(self).`id`) like `AppUser%`)
We are then trying to run the following query:
SELECT RAW `Extent1`
FROM `Portal` as `Extent1`
USE INDEX (`AppUser_SubjectId3` USING GSI)
WHERE (`Extent1`.`SubjectId` = 'c8ea08231c3a985a06342355b87d6e2d6290a985d5c3592e5b8e5e5f14608a08')
And get the following error:
No index available on keyspace Portal that matches your query.
Use CREATE INDEX or CREATE PRIMARY INDEX to create an index,
or check that your expected index is online.
We have confirmed the obvious that the index is online. The only item worth noting is that the Bucket does not actually contain any documents at the moment, but we would not expect this error in this instance, simply nothing to be returned.
Any ideas?
EDIT:
I have created another index without the WHERE clause and it does not return the error any longer.
CREATE INDEX `AppUser_SubjectId4` ON `Portal`(`SubjectId`)
The only problem is that the WHERE clause is required!
The Index You created is partial index (i.e Index has WHERE clause, only has entries that satisfies where condition). For query to use that index it must qualify (i.e query where clause must be subset of index where clause + query predicate must have leading index key) other wise by choosing that index it can result in wrong results and query optimizer will not choose that index.
Also like AppUser% is incorrect it must be single or double quotes not back-ticks.
CREATE INDEX `AppUser_SubjectId3` ON `test`(`SubjectId`)
WHERE meta().`id` like "AppUser%";
SELECT RAW e
FROM `Portal` AS e
USE INDEX (`AppUser_SubjectId3` USING GSI)
WHERE e.`SubjectId` = 'c8ea08231c3a985a06342355b87d6e2d6290a985d5c3592e5b8e5e5f14608a08'
AND META(e).id LIKE "AppUser%";
Designing Index For Query In Couchbase N1QL https://blog.couchbase.com/wp-content/uploads/2017/10/N1QL-A-Practical-Guide-2nd-Edition.pdf
I tried reproducing your problem successfully, with an empty bucket named test.
I created the same index with the following query :
CREATE INDEX `AppUser_SubjectId3` ON `test`(`SubjectId`) WHERE ((meta(self).`id`) like `AppUser%`)
And checked that the index was online. I then tried your query, which resulted with the exact same error.
[
{
"code": 4000,
"msg": "No index available on keyspace test that matches your query. Use CREATE INDEX or CREATE PRIMARY INDEX to create an index, or check that your expected index is online.",
"query_from_user": "SELECT RAW `Extent1` FROM `test` as `Extent1` USE INDEX (`AppUser_SubjectId3` USING GSI) WHERE (`Extent1`.`SubjectId` = 'c8ea08231c3a985a06342355b87d6e2d6290a985d5c3592e5b8e5e5f14608a08')"
}
]
So I checked the bucket's schema with :
INFER `test`
This returned the following error :
[
{
"code": 0,
"msg": "Keyspace test has no documents, schema inference not possible"
}
]
However I also created a primary index, and querying it would just return an empty results array.
Now I don't really know where to go from this, but it does seem like querying an empty bucket using anything else than a primary index would return this error.
This issue is resolved for me just creating an index over bucket without using any where clause.
I have created a table and I wish to make a Computed Column from the concatenated values of three other fields in the table.
I want this Computed Field to take place at INSERT or UPDATE, so I am specifying PERSISTENT
I have tried the following code (in various ways) in phpMyAdmin but always get errors, which seem to be referencing immediately after ALTER table
I did not see a way of doing this when adding the field in phpMyAdmin, so I hoped I could ALTER it.
Alter TABLE 'tlImages'
CHANGE COLUMN tlImageQuery
AS CONCAT(tlImgTitle,"~",tlImgDescrip,"~",tlImgWhereWhen) PERSISTENT;
MariaDB version 10.0.29-MariaDB-cll-lve - MariaDB Server
phpMyAdmin . Version information: 4.0.10.18
First, lose single quotes around the table name, they are not suitable for this purpose. Use backticks or nothing.
You will still get a syntax error further in the statement, because AS clause should be in brackets. Add them.
You will still get a syntax error because you are missing column type before the AS (...) clause, add it.
You will still get a syntax error because CHANGE COLUMN needs two column names, old and new, use MODIFY instead.
Alter TABLE `tlImages`
MODIFY COLUMN tlImageQuery VARCHAR(128)
AS (CONCAT(tlImgTitle,"~",tlImgDescrip,"~",tlImgWhereWhen)) PERSISTENT
;
(Type VARCHAR(128) is given just as an example).
Does sql keep a record of composite keys or does it calculate them each time a record is inserted/deleted/updated...?
If it does is there a way to call it without having to get each member field value, something like ...WHERE composite_pk=CONCAT('value1','value2','value3')
That is correct, composite index is updated when field value is changed. But index has to be unique, otherwise MySql won't allow you to save a changed value. (you'll see an error: #1062 - Duplicate entry 'a-b-c' for key 'x')
Index can't be used in WHERE statement.
useful read: http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html
Also about index Hinting (use, ignore, force): http://dev.mysql.com/doc/refman/5.1/en/index-hints.html
searching around trying to find a way to add index on suffix of a column I couldn't find a solution.
The only thing for partial index which I found is on prefix of index which is:
CREATE INDEX part_of_name ON customer (name(10));
from 12.1.13. CREATE INDEX Syntax
I also tried other stuff like:
CREATE INDEX part_of_name ON customer (name(-10));
CREATE INDEX part_of_name ON customer (RIGHT(name,10));
CREATE INDEX part_of_name ON customer (SUBSTRING(name,-10));
ALL with same error:
ERROR 1064 (42000): You have an error in your SQL syntax;
I would like your help!
Thank you in advance!
MySQL doesn't support indexes on arbitrary expressions. The prefix index is only a quirk, since it's "free" in a typical implementation of B-Tree indexes.
You'll need to add another column to the table, populate it with a trigger, and put index on that.