query-optimization

What is the best way to implement a substring search in SQL?

做~自己de王妃 提交于 2019-11-27 01:29:47
问题 We have a simple SQL problem here. In a varchar column, we wanted to search for a string anywhere in the field. What is the best way to implement this for performance? Obviously an index is not going to help here, any other tricks? We are using MySQL and have about 3 million records. We need to execute many of these queries per second so really trying to implement these with the best performance. The most simple way to do this is so far is: Select * from table where column like '%search%' I

How can I further optimize a derived table query which performs better than the JOINed equivalent?

折月煮酒 提交于 2019-11-27 01:06:43
问题 UPDATE: I found a solution. See my Answer below. My Question How can I optimize this query to minimize my downtime? I need to update over 50 schemas with the number of tickets ranging from 100,000 to 2 million. Is it advisable to attempt to set all fields in tickets_extra at the same time? I feel that there is a solution here that I'm just not seeing. Ive been banging my head against this problem for over a day. Also, I initially tried without using a sub SELECT, but the performance was much

PostgreSQL query runs faster with index scan, but engine chooses hash join

…衆ロ難τιáo~ 提交于 2019-11-27 00:46:00
问题 The query: SELECT "replays_game".* FROM "replays_game" INNER JOIN "replays_playeringame" ON "replays_game"."id" = "replays_playeringame"."game_id" WHERE "replays_playeringame"."player_id" = 50027 If I set SET enable_seqscan = off , then it does the fast thing, which is: QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=0.00..27349.80 rows=3395 width

Difference in MySQL JOIN vs LEFT JOIN

China☆狼群 提交于 2019-11-27 00:44:33
I have this cross-database query... SELECT `DM_Server`.`Jobs`.*, `DM_Server`.servers.Description AS server, digital_inventory.params, products.products_id, products.products_pdfupload, customers.customers_firstname, customers.customers_lastname FROM `DM_Server`.`Jobs` INNER JOIN `DM_Server`.servers ON servers.ServerID = Jobs.Jobs_ServerID JOIN `cpod_live`.`digital_inventory` ON digital_inventory.jobname = Jobs.Jobs_Name JOIN `cpod_live`.`products` ON products.products_pdfupload = CONCAT(digital_inventory.jobname, ".pdf") JOIN `cpod_live`.`customers` ON customers.customers_id = products.cID

MySQL explain Query understanding

江枫思渺然 提交于 2019-11-27 00:06:13
问题 I've read on some blogs and in some articles related to optimization, how to optimize queries. I read I need to use indexes and make sure all my primary key and foreign keys are set correctly using a good relational database schema. Now I have a query I need to optimize and I get this on the EXPLAIN : Using where; Using temporary; Using filesort I am using MySQL 5.5 I know I am using WHERE but not with my temporary table nor filesort? What does this mean? 回答1: Using temporary means that MySQL

Is there an “Explain Query” for MongoDB Linq?

若如初见. 提交于 2019-11-26 22:18:43
问题 Is there a way to run .explain() or equivalent on Linq queries? I would want to know The text of the actual JSON query The output of .explain() (indexes used, etc) It would also be nice to have the execution time of the query 回答1: You can get the Json easily enough if you have a query wrapper; var qLinq = Query<T>.Where(x => x.name=="jim"); Console.WriteLine(qLinq.ToJson()); There's also an Explain() method on MongoCursor, so you could do this; var exp = Collection.FindAs<T>(qLinq).Explain()

Mysql optimization for REGEXP

≡放荡痞女 提交于 2019-11-26 22:03:50
问题 This query (with different name instead of "jack") happens many times in my slow query log. Why? The Users table has many fields (more than these three I've selected) and about 40.000 rows. select name,username,id from Users where ( name REGEXP '[[:<:]]jack[[:>:]]' ) or ( username REGEXP '[[:<:]]jack[[:>:]]' ) order by name limit 0,5; id is primary and autoincrement. name has an index. username has a unique index. Sometimes it takes 3 seconds! If I explain the select on MySQL I've got this:

Checking multiple columns for one value

[亡魂溺海] 提交于 2019-11-26 21:54:05
I have a table that has columns like this for example: id,col1,col2,col3,col4 Now, I want to check if ANY of col1, col2, col3, col4 have the passed in value. The long way to do it would be.. SELECT * FROM table WHERE (col1 = 123 OR col2 = 123 OR col3 = 123 OR col4 = 123); I guess it's the opposite version of IN . Is there an easier way to do what I want? You can use the IN predicate, like so: SELECT * FROM table WHERE 123 IN(col1, col2, col3, col4); SQL Fiddle Demo it's the opposite version of IN. No it is not , It is the same as using the OR s the way you did in your question. To clarify this

mysql count performance

谁说胖子不能爱 提交于 2019-11-26 21:25:10
问题 select count(*) from mytable; select count(table_id) from mytable; //table_id is the primary_key both query were running slow on a table with 10 million rows. I am wondering why since wouldn't it easy for mysql to keep a counter that gets updated on all insert,update and delete? and is there a way to improve this query? I used explain but didn't help much. 回答1: As cherouvim pointed out in the comments, it depends on the storage engine. MyISAM does keep a count of the table rows, and can keep

OR Operator Short-circuit in SQL Server

喜欢而已 提交于 2019-11-26 21:01:09
I want to consult SQL Server OR short-circuit Code: DECLARE @tempTable table ( id int ) INSERT @tempTable(id) values(1) DECLARE @id varchar(10) SET @id = 'x' SELECT * FROM @tempTable WHERE 1=1 OR id = @id --successfully SELECT * FROM @tempTable WHERE @id = 'x' OR id = @id --Exception not Convert 'x' to int Why? 1=1 and @id='x' is true. SQL Server OR operator : whether the short-circuit function? THANKS Within SQL, there is no requirement that an OR clause breaks early. In other words, it is up to the optimizer whether to check both conditions simutaneously. I am not an expert in the MSSQL