How I can do this in mysql? - mysql

I want to make a query that select fields using 'like' but I am not satisfied with the result, for example my register says, "wood table work 45" but if my query is SELECT * FROM schema1.table1 WHERE description LIKE "%table for work%"; returns nothing, I don't want the user need write exactly "table work" or "wood table" to have a result.

Create first a FULL TEXT Index on the column you wants to query this way :
ALTER TABLE `table1`
ADD FULLTEXT INDEX `IndxDescription` (`description`);
You don't need to run an indexer daemon, MySQL does the index / reindex automatically.
FYI: Full-text is supported in InnoDB / MyISAM only in MySQL
reference : http://dev.mysql.com/doc/refman/5.7/en/fulltext-restrictions.html
If you are looking for a more robust solution, consider taking a look on ElasticSearch : https://github.com/elastic/elasticsearch

You might want to look at Natural Language Fulltext search

Related

How To Use MySQL Indexes Properly?

I'm using php and MySQL with a rather large MySQL database and I am trying to use idexes for the first time. I get the concept that the server will look through the index first but I'm moving trouble getting the server to use the index. So my questions are:
Does having a primary key (thus primary index?) in the table get used over the index I'm trying to use from another column? Do I have to explicitly specify the index in the select query? (I'm using several table joins, btw)
Does anyone know of a good beginners guide to using MySQL indexes? I haven't found a good one!
An important rule for mysql indexes, say you have the following index:
KEY(A,B,C)
Then in your code you have a where clause or join such as:
WHERE B = 'mydata' AND C = 'moredata'
Mysql will not be able to make use of the index for the query because the indexes work in the order given and the column A has not been included.
The fix is to either use A in the query or re-order the index (or add a second index) as so:
KEY(C,B,A)
or (if you don`t need A at all in the WHERE/JOIN):
KEY(C,B)
Also check out explain which should help you find why your indexes are not being used.

Replacing table with view in MySQL

I have to handle database that is read (and only read) by third party software I cannot verify. It has a table which stores partial copy of other table.
Would it be safe to replace this table with view?
Besides the view restrictions, when using views in MySQL you have to be aware of one very important issue: The performance of WHERE statements suffers greatly:
For example:
SELECT column_a FROM table_n WHERE column_a="some value";
is quick assuming an index is in place on column_a.
Now create a view:
CREATE VIEW column_a_view AS SELECT column_a FROM table_n;
SELECT * FROM column_a_view b WHERE b.column_a="some value";
can lead to a full table scan since MySQL (at least until 5.6) did not always recognize the fact that it can use the index.
So especially if you are working with large tables, it can be more beneficial to create "copies" of the related data and replace that data once per time interval then working with views.

Mysql Explain Select "operation", "key_len" and "ref"

I have two identical InnoDB tables
Second table was created with
CREATE TABLE second LIKE first;
INSERT INTO second SELECT * FROM first;
Okay.
But when I'm trying to EXPLAIN SELECT it shows me this for first table:
type=ref, key_len=4, ref=const
for second:
type=range, key_len=16, ref=NULL
all other values are identical.
Need to say, that SELECT from second table works much faster than SELECT from first canonical table.
The indices are all the same in first and second tables.
If you need more details I can give it.
UPD: the first table has got Triggers on it, but it will fire only when insert/update will run.
UPD2: no one table uses Memory access methods
FORCE KEY was resolved my problem
MySQL 5.5 Percona

How to hint the index to use in a MySQL select query?

I have a MySQL query (running MySQL 5.0.88), which I'm trying to speed up. The underlying table has multiple indices and for the query in question, the wrong index is used (i_active - 16.000 rows, vs. i_iln - 7 rows).
I'm not very experienced with MySQL but read there is a use index hint, which can force mySQL to use a certain index. I'm trying it like this:
SELECT art.firma USE INDEX (i_iln)
...
but this produces a MySQL error.
Question:
Can anyone tell me what I'm doing wrong? (Except running 5.0.88, which I can't change.)
You missed the
FROM table
Correct SQL should be:
SELECT art.firma FROM your_table USE INDEX (i_iln) WHERE ....
select * from table use index (idx);
http://dev.mysql.com/doc/refman/5.0/en/index-hints.html
sometimes, with use index (index_name) optimizer might go for table scan, if you use hint force index, optimizer will be forced to use index, will go for table scan only if no ways left to get the rows with provided index.
SELECT art.firma FROM art FORCE INDEX (i_iln);
for more detail on hints USE INDEX and FORCE INDEX check this link
Select Coloumn1,Coloumn2,Coloumn.... FROM TABLE_NAME USE INDEX(index_name)
WHERE Coloumn="condition";
if you have correct index thn you dnt need to use index(). your query automic select correct index.If your query slow after using index thn recheck your index ,something wrong in index.
thanks in advance.enter code here

Full text search with a very simple MySQL statement

I recently came across a wierd issue with MySQL Fulltext search. My statement is really simple:
SELECT * FROM `mytable` WHERE MATCH (`desc`) AGAINST ('+NOR +710' IN BOOLEAN MODE)
And this is what in the desc column: "The NOR 710 also has smoke seal ..."
For some reason it won't find that row. I added Fulltext index to that column, mysql version is 5.1.56 , database engine of that table is MyISAM. Is there anything else i need to check?
Thanks
By default, fulltext indexes will ignore words that are shorter than 4 charaters. Adjust your ft_min_word_len to also include the shorter words.