query-optimization

MySQL stops using index when additional constraints are added

半世苍凉 提交于 2019-12-11 01:03:19
问题 Using EXPLAIN reveals that the following query does not use my index, could somebody please explain what is going on? SELECT u.id AS userId, firstName, profilePhotoId, preferredActivityId, preferredSubActivityId, availabilityType, 3959 * ACOS(COS(radians(requestingUserLat)) * COS(radians(u.latitude)) * COS(radians(u.longitude) - radians(requestingUserLon)) + SIN(radians(requestingUserLat)) * SIN(radians(u.latitude))) AS distanceInMiles FROM users u WHERE u.latitude between lat1 and lat2 --

MySQL: why 'explain' command yields different results on same SQL statement?

我怕爱的太早我们不能终老 提交于 2019-12-11 00:07:43
问题 I migrated a MySQL database from one environment to another, and discovered that after migrating, a particular query runs extremely slower than it was. I was investigating the statement with 'explain' keyword (below) and found that the 'explain' command gives different output, on different servers. explain select distinct j.job,f.path,p.path from fixes f join jobs j on f.job=j.id join paths p on p.id =f.path where p.path like '//depot1/Dev\-trunk/%' ; For the original one, it gives: 1 SIMPLE

Query where two columns are in the result of nested query

我与影子孤独终老i 提交于 2019-12-10 18:47:29
问题 I'm writing a query like this: select * from myTable where X in (select X from Y) and XX in (select X from Y) Values from columns X and XX has to be in the result of the same query: select X from Y . I think that this query is invoked twice so its senseless. Is there any other option I can write this query more efficiently? Maybe temp table? 回答1: Actually no, there isn't a smarter way to write this (without visiting Y twice) given the X that myTable.X and myTable.YY matches to may not be from

How do I form a query with a running count retaining the order

瘦欲@ 提交于 2019-12-10 18:20:02
问题 I have a trace table which looks like this I'd like to get a running total which looks like the following output - its very important that I retain the order - as this is the execution order of the stored porcedures - It will help me analyze bottle necks in the system I have tried select max(RowNumber),objectname, count(1) from rob where eventclass = 42 group by objectname But that mucks up the order Is this even possible in SQL? UPDATE: I tried this select RowNumber,objectname, count(1) from

How to limit query results to exact group match

最后都变了- 提交于 2019-12-10 18:07:50
问题 I have a table like the following: user | item ------------- X | Apple X | Orange X | Pear Y | Orange Y | Pear Z | Apple Z | Orange My goal is to have 3 search options: ANY , ALL (At Least), EXACT Where ANY returns a list of users who have at least one item searched for, so searching for "Apple" - ANY would return X,Z, searching for "Apple, Orange" - ANY would return X,Y,Z ALL returns a list of users who have all items searched for, so searching for "Apple" - ALL would return X,Z, searching

XQUERY SQL - How to pass individual node element(s) as variables?

核能气质少年 提交于 2019-12-10 17:56:14
问题 Is there an alternate way to pass node elements dynamically than the one shown below - ? select XMLTable.XMLCOL.query('//*[local-name()=sql:variable("@node")') For e.g., even if I am trying to give the fully qualified path, I do not want to hard code the node elements, instead I would like to pass them individually as parameters. Under the section - Example: Query Using sp_executesql @ http://msdn.microsoft.com/en-us/library/ms345118(v=sql.90).aspx it says - query contains wildcards (*) and

MySQL MyISAM table performance… painfully, painfully slow

本秂侑毒 提交于 2019-12-10 17:45:33
问题 I've got a table structure that can be summarized as follows: pagegroup * pagegroupid * name has 3600 rows page * pageid * pagegroupid * data references pagegroup; has 10000 rows; can have anything between 1-700 rows per pagegroup; the data column is of type mediumtext and the column contains 100k - 200kbytes data per row userdata * userdataid * pageid * column1 * column2 * column9 references page; has about 300,000 rows; can have about 1-50 rows per page The above structure is pretty

Optimizing a simple query on two large tables

故事扮演 提交于 2019-12-10 16:38:47
问题 I'm trying to offer a feature where I can show pages most viewed by friends. My friends table has 5.7M rows and the views table has 5.3M rows. At the moment I just want to run a query on these two tables and find the 20 most viewed page id's by a person's friend. Here's the query as I have it now: SELECT page_id FROM `views` INNER JOIN `friendships` ON friendships.receiver_id = views.user_id WHERE (`friendships`.`creator_id` = 143416) GROUP BY page_id ORDER BY count(views.user_id) desc LIMIT

How to eliminate duplicate calculation in SQL?

时间秒杀一切 提交于 2019-12-10 16:37:39
问题 I have a SQL that can be simplified to: SELECT * FROM table WHERE LOCATE( column, :keyword ) > 0 ORDER BY LOCATE( column, :keyword ) You can see there is a duplicate of "LOCATE( column, :keyword )". Is there a way to calculate it only once ? 回答1: HAVING works with aliases in MySQL: SELECT *, LOCATE( column, :keyword ) AS somelabel FROM table HAVING somelabel > 0 ORDER BY somelabel 回答2: SELECT *, LOCATE( column, :keyword ) AS somelabel FROM table WHERE somelabel > 0 ORDER BY somelabel 回答3:

What will happen if I kill a huge MySQL InnoDb DELETE Query?

陌路散爱 提交于 2019-12-10 15:24:16
问题 I'm currently running a DELETE query that is taking a lot longer than expected (already 10hrs!). I would like to kill it through phpmyadmin processes, however am concerned about what might happen. Will the roll-back he automatically does take a lot of time also? Current query status shows "updating". 回答1: It depends on the stage your query is in right now. But generally rollback takes about equal time, sometimes even more than the original operation. As per point 2 of this document, it's not