query-optimization

SQL question from Joel Spolsky article

一曲冷凌霜 提交于 2019-12-05 00:48:08
From Joel Spolsky's article on leaky abstractions: [C]ertain SQL queries are thousands of times slower than other logically equivalent queries. A famous example of this is that some SQL servers are dramatically faster if you specify "where a=b and b=c and a=c" than if you only specify "where a=b and b=c" even though the result set is the same. Does anyone know the details of this? Obviously, a = b and b = c => a = c - this is related to transitive closure. The point Joel was making is that some SQL servers are poor at optimizing queries, so some of the SQL queries might be written with "extra"

MySQL: Why does an Order By ID runs much slower than Order By other Columns?

纵然是瞬间 提交于 2019-12-04 23:58:14
I am using MySQL version 5.5.14 to run the following query, QUERY 1 , from a table of 5 Million rows: SELECT P.ID, P.Type, P.Name, P.cty , X(P.latlng) as 'lat', Y(P.latlng) as 'lng' , P.cur, P.ak, P.tn, P.St, P.Tm, P.flA, P.ldA, P.flN , P.lv, P.bd, P.bt, P.nb , P.ak * E.usD as 'usP' FROM PIG P INNER JOIN EEL E ON E.cur = P.cur WHERE act='1' AND flA >= '1615' AND ldA >= '0' AND yr >= (YEAR(NOW()) - 100) AND lv >= '0' AND bd >= '3' AND bt >= '2' AND nb <= '5' AND cDate >= NOW() AND MBRContains(LineString( Point(39.9097, -2.1973) , Point(65.5130, 41.7480) ), latlng) AND Type = 'g' AND tn = 'l'

Getting rid of “Using temporary; Using filesort”

爱⌒轻易说出口 提交于 2019-12-04 19:38:31
问题 When I do an explain on my query I see that it has "Using temporary; Using filesort" under "Extra" for the first row. I understand this is bad but I don't know what exactly it means or how to fix it. If you want to see my query, here's a more general question I asked about the same query: MySQL query optimization and EXPLAIN for a noob. For reference, the query involves 24 tables and 23 joins. My questions now are: What do "Using temporary" and "Using filesort" mean? Assuming they're bad, how

how to optimize mysql query if i have too many OR operators in my query?

ε祈祈猫儿з 提交于 2019-12-04 19:18:20
using PHP and MySQL i have grabbed an array of facebook user ids from facebook. Now i want to find the corresponding username in my application for this array. Clearly in my application the user table contains unique username and unique fb_uid values. my rudimentary understanding oof programming led me to 2 ways: 1) use a loop and run through the array of fb_uid and find the username one by one. OR 2) create a monster query like select distinct(username) from users where fb_uid = value1 OR fb_uid = value2 ... so is there a better way out? Thank you. Use SQL's IN operator instead: select

SQL ROW_NUMBER() over performance problem

删除回忆录丶 提交于 2019-12-04 16:55:17
I have this SQL that works fine. Want the my filter to return the LATEST unique SessionGuids with the highest UserSessionSequenceID. Problem is performance sucks - even though I have good indexes. How can I rewrite this - to omit the ROW_NUMBER line? SELECT TOP(@resultCount) * FROM ( SELECT [UserSessionSequenceID] ,[SessionGuid] ,[IP] ,[Url] ,[UrlTitle] ,[SiteID] ,[BrowserWidth] ,[BrowserHeight] ,[Browser] ,[BrowserVersion] ,[Referer] ,[Timestamp] ,ROW_NUMBER() over (PARTITION BY [SessionGuid] ORDER BY UserSessionSequenceID DESC) AS sort FROM [tblSequence] ) AS t WHERE ([Timestamp] > DATEADD

Query performance of combined index vs. multiple single indexes vs. fulltext index

半城伤御伤魂 提交于 2019-12-04 14:06:53
问题 Background: I have a table with 5 million address entries which I'd like to search for different fields (customer name, contact name, zip, city, phone, ...), up to 8 fields. The data is pretty stable, maximum 50 changes a day, so almost only read access. The user isn't supposed to tell me in advance what he's searching for, and I also want support of combined search (AND-concatenation of search terms). For example "lincoln+lond" should search for all records containing both search terms in

How to optimise the 'XQuery' SQL

两盒软妹~` 提交于 2019-12-04 12:23:42
I have an XML hierarchy like this in an XML type column of a table with 10,000 records- <Root> <Elem1> <Parent1> <Separator> <Child1/> </Separator> </Parent1> </Elem1> </Root> I have a query like this - DECLARE @Root VARCHAR(50) DECLARE @Entity VARCHAR(50) DECLARE @ParentNode VARCHAR(50) DECLARE @Separator VARCHAR(50) DECLARE @ChildNode VARCHAR(50) SET @Root = 'Root' SET @Entity = 'Elem1' SET @ParentNode = 'Parent1' SET @Separator = 'separator' SET @ChildNode = 'Child1' select Parent.P.value('.', 'varchar(max)') as MyValue, T.uniqueId, T.XMLCol from [XMLTable] as T cross apply (SELECT XMLTable

MySQL index for MIN and MAX

橙三吉。 提交于 2019-12-04 10:56:22
Could anyone clarify this point from the official MySQL documentation Indexes are used ... To find the MIN() or MAX() value for a specific indexed column key_col. This is optimized by a preprocessor that checks whether you are using WHERE key_part_N = constant on all key parts that occur before key_col in the index. In this case, MySQL does a single key lookup for each MIN() or MAX() expression and replaces it with a constant. If all expressions are replaced with constants, the query returns at once. For example: SELECT MIN(key_part2),MAX(key_part2) FROM tbl_name WHERE key_part1=10; So in

Optimising MySQL queries across hierarchical data

本秂侑毒 提交于 2019-12-04 10:22:30
问题 I have a fairly stable directed graph of order ~100k vertices and size ~1k edges. It is two-dimensional insofar as its vertices can be identified by a pair of integers (x, y) (of cardinality ~100 x ~1000) and all edges are strictly increasing in x . There is furthermore a dictionary of ~1k (key, val) pairs associated with each vertex. I am currently storing the graph in a MySQL database across three (InnoDB) tables: a table of vertices (which I don't think is relevant to my question, so I

Is storing counts of database record redundant?

孤人 提交于 2019-12-04 08:52:16
I'm using Rails and MySQL, and have an efficiency question based on row counting. I have a Project model that has_many :donations . I want to count the number of unique donors for a project. Is having a field in the projects table called num_donors , and incrementing it when a new donor is created a good idea? Or is something like @num_donors = Donor.count(:select => 'DISTINCT user_id') going to be similar or the same in terms of efficiency thanks to database optimization? Will this require me to create indexes for user_id and any other fields I want to count? Does the same answer hold for