ms sql search by nvarchar field - search

I am using MS SQl 2012 and have simple table
id (int) - primary key
fullname (nvarchar(500)
age (int)
…
In table over 15 millions records and I need make simple search like
select * from Customers where full name like '%sometext%'
Details:
1. It can have few words
2. There are used many languages
3. that fullname doesn't have any indexes yet
What the best way make search like that? Which indexes I should add? Can I use full text search if there are not only english words?

Sargable The above query does not meet the requirements therefore no index on your full name column is going to help. You will need to restructure your data or filter on something sargable first (age = 30) and then scan full name.
The other option is to pull in a text searching technology as #afrancis indicated.

Related

Cassandra: filtering based one specific value in a set

I have a data table in Cassandra and one of the columns is:
customer_favourites, with each value being of type set and it has the details of each customer's favourite foods. For example one customer could have {'Mexican', 'Italian', 'Indian'} and another customer could have {'Mexican', 'French'} and another could have {'Mexican'}.
I have the following code:
SELECT customer_id, customer_fname, customer_lname FROM customers WHERE customer_favourites CONTAINS ‘Mexican’ ALLOW FILTERING;
I want it to filter on those customers whose favourite food is ONLY Mexican, but right now it's returning the details of every customer who has Mexican as one of their favourite foods. How do I filter my query to return customer who like ONLY Mexican food?
Naive approach: You need to use customer_favourites = {'Mexican'}...
Better approach - create secondary index on the corresponding field, using the FULL keyword, and then use customer_favourites = {'Mexican'}.
Best approach - create a separate table with customer_favourites as partition key, and search users in it (column should be frozen). One of the problems with this approach would be the data skew, as number of favorite foods is relatively small, and quite imbalanced.
Alternative approach - reconsider the use of the Cassandra, if you need to search by non-partition key very often.

number listing for conditions in sqlite

I have a table in which I am trying to create a column that will increment based on conditions being valid.
In my example, I need to update the department count where the department = Marketing. Anyone have a good way to do this using SQLite?
Current Table
Name Department Department_Count
James Accounting NULL
Jennifer Marketing NULL
Micheal Warehouse NULL
Natalie Marketing NULL
Rebecca Marketing NULL
Update Table
Name Department Department_Count
James Accounting NULL
Jennifer Marketing 1
Micheal Warehouse NULL
Natalie Marketing 2
Rebecca Marketing 3
Edit:
Currently, I insert rows where the department is 'Marketing' into a new table and then I used primary key or rowid to create an auto increment so I can number these types of items.
This requires me to create a new table which is not the best since it takes up so much space and the table is redundant since I have the underlying data in this original table already.
I'm using python to interact with my database, if that helps with solving this problem.
UPDATE
Actually, thinking about it a little further, you may not need a trigger:
INSERT INTO Table (Department, Department_Count)
VALUES (?, (SELECT MAX(IFNULL(Department_Count, 0)+1 FROM Table WHERE Department = ?))
may give you what you want.
Original Answer
You cannot do this declaratively, but you can probably accomplish what you want proceduraly using a trigger.
Two possible strategies:
Use an AFTER INSERT trigger to execute an UPDATE statement against the most recently inserted row (RowID will be available AFTER INSERT) to set the Departement_Count column to a SELECT expression based on the current data in the table.
Use an INSTEAD OF trigger to perform an alternate INSERT combining the values from the NEW cursor with a similar SELECT statement to get the maximum value (plus 1) from the Department_Count column.

Cassandra: How to check if a column value is already present

I am using Cassandra. There is a column name-text which stores all say usernames.
name
------
bob
Bob
bobby
mike
michael
micky
BOB
I have 2 questions
I have to select all user names that starts with 'bo'. I know there is no like equivalent in Cassandra. But is there anyway to achieve that? (Additional column is an option but is there something else?)
There are 3 entries. bob,Bob and BOB. Is there anyway to use fetch all 3 rows if I pass where name='bob'. I need to fetch the names case-insensitive.
Thanks in advance.
Let's start with the second question first. If you want to support case-insensitive queries, you should store a second, upper-case copy of the text data you want to search for in another column. Then by querying by that column you'll be able to do case-insensitive requests.
Going back to searches for bo*. The best way to do that is to use a schema that allows you to leverage clustering columns (columns 2 and higher of the primary key) for range searches. Here is an example:
CREATE TABLE t1 (region INT, name TEXT, PRIMARY KEY (region, name));
In particular, if you make name the second column of the key, you will be able to perform searches such as
SELECT * FROM t1 WHERE name >= 'bo' and name < 'bp' ALLOW FILTERING;
which will return results you're looking for. This only works for trailing wildcards: the leading characters have to be constant for range queries to work. Again, if you want to do case-insensitive searches, have the case-insensitive column be the second part of the primary key and query by it:
SELECT * FROM t1 WHERE name_upper >= 'BO' and name_upper < 'BP' ALLOW FILTERING;

Search technique in Mysql using only sql not pl/sql

Here i want to search from one table's column to another table's column.
so if the string is "Introduction To Php" so this string will compare to another table all data for particular column but the new things is order of words may be different in table.
i.e : it can be like "Php introduction to" , "to indtroduction php" and so on... means combination of different words and there place in the string should be compare with
If you have any idea related this , please answer for this question.
Thanks a lot in advance..
You may be interested in full-text search functions in MySQL http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html

Order SharePoint search results by more columns

I'm using a FullTextSqlQuery in SharePoint 2007 (MOSS) and need to order the results by two columns:
SELECT WorkId FROM SCOPE() ORDER BY Author ASC, Rank DESC
However it seems that only the first column from ORDER BY is taken into account when returning results. In this case the results are ordered correctly by Author, but not by Rank. If I change the order the results will be ordered by Rank, but not by Author.
I had to resort to my own sorting of the results, which I don't like very much. Has anybody a solution to this?
Edit: Unfortunately it also doesn't accept expressions in the ORDER BY clause (SharePoint throws an exception). My guess is that even if the query looks like legitimate SQL it is parsed somehow before being served to the SQL server.
I tried to catch the query with SQL Profiler, but to no avail.
Edit 2: In the end I used ordering by a single column (Author in my case, since it's the most important) and did the second ordering in code on the TOP N of the results. Works good enough for the project, but leaves a bad feeling of kludgy code.
Microsoft finally posted a knowledge base article about this issue.
"When using RANK in the ORDER BY clause of a SharePoint Search query, no other properties should be used"
http://support.microsoft.com/kb/970830
Symptom: When using RANK in the ORDER BY clause of a SharePoint Search query only the first ORDER BY column is used in the results.
Cause: RANK is a special property that is ranked in the full text index and hence cannot be used with other managed properties.
Resolution: Do not use multiple properties in conjunction with the RANK property.
Rank is a special column in MOSS FullTextSqlQuery that give a numeric value to the rank of each result. That value will be different for each query, and is relative to the other results for that particular query. Because of this rank should have a unique value for each result, and sorting by rank then author would be the same as just sorting by rank. I would try sorting on another column instead of rank to see if results come back as you expect, if so your trouble could be related to the way MOSS is ranking the results, which will vary for each unique query.
Also you are right, the query looks like SQL, but it is not the query actually passed to the SQL server, it is special Microsoft Enterprise Search SQL Query syntax.
I, too, am experiencing the same problem with FullTextSqlQuery and MOSS 2007 where only the first column in a multi-column "ORDER BY" is respected.
I entered this topic in the MSDN Forums for SharePoint Search, but have not received any replies:
http://social.msdn.microsoft.com/Forums/en-US/sharepointsearch/thread/489b4f29-4155-4c3b-b493-b2fad687ee56
I have no experience in SharePoint, but if it is the case where only one ORDER BY clause is being honored I would change it to an expression rather than a column. Assuming "Rank" is a numeric column with a maximum value of 10 the following may work:
SELECT WorkId FROM SCOPE() ORDER BY AUTHOR + (10 - Rank) ASC

Resources