query-optimization

How to return rows listed in descending order of COUNT(*)?

早过忘川 提交于 2019-12-01 01:51:54
问题 I have a table called foo with these fields: - id - type - parentId I want to select a list of parent IDS, in the descending order of their COUNT(*) of how many times they appear in the table. Something like this: SELECT DISTINCT parentId FROM `foo` ORDER BY (COUNT(parentId) DESC where parentId = parentId) How can this be done in the most efficient way and putting the least load on the server? There can be thousands-hundreds of thousands of records in the table, so manually going through each

How to optimize SQL query with calculating distance by longitude and latitude?

久未见 提交于 2019-12-01 00:14:51
I have a table with structure like that: table name: shop id_shop int(10) name varchar(200) latitude double longitude double And I'd like to calculate distance between given coordinates and coordinates saved in database. My current query: SELECT * FROM `shop` AS `s` WHERE ( ( 6371 * ACOS( SIN( RADIANS( latitude ) ) * SIN( RADIANS( 53.5353010379 ) ) + COS( RADIANS( latitude ) ) * COS( RADIANS( 53.5353010379 ) ) * COS( RADIANS( 14.7984442616 ) - RADIANS( longitude ) ) ) ) <= 25 ) plus some JOIN LEFT 's for some data. Is there any way to optimize that query ? With joins it takes about 13msec. I

Count search criteria record based on search done by user (MYSQL PHP)

偶尔善良 提交于 2019-11-30 23:53:58
I have a search form which provides searching properties for holiday in a specific country based on it's availability specific date. Search section has 2 sections "basic search" & "advance search". Basic search contains country dropdown and date field. In advance search we have multiple filters for hotels like "Bedrooms" (1 bedroom, 2 bedroom etc) and then property type (apartment, villa, etc) I want to show the search filter options with a count such as "1 bedroom (23 properties)" and same for other search filter options. I am using php/mysql to create this application, so what comes first in

Need help optimizing a lat/Lon geo search for mysql

為{幸葍}努か 提交于 2019-11-30 22:46:23
I have a mysql (5.0.22) myisam table with roughly 300k records in it and I want to do a lat/lon distance search within a five mile radius. I have an index that covers the lat/lon fields and is fast (milisecond response) when I just select for lat/lon. But when I select for additional fields in the table is slows down horribly to 5-8 seconds. I'm using myisam to take advantage of fulltext search. The other indexes perform well (e.g. select * from Listing where slug = 'xxxxx'). How can I optimize my query, table or index to speed things up? My schema is: CREATE TABLE `Listing` ( `id` int(10)

Count search criteria record based on search done by user (MYSQL PHP)

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 18:58:00
问题 I have a search form which provides searching properties for holiday in a specific country based on it's availability specific date. Search section has 2 sections "basic search" & "advance search". Basic search contains country dropdown and date field. In advance search we have multiple filters for hotels like "Bedrooms" (1 bedroom, 2 bedroom etc) and then property type (apartment, villa, etc) I want to show the search filter options with a count such as "1 bedroom (23 properties)" and same

SPARQL Speed up federated query

梦想与她 提交于 2019-11-30 18:04:42
问题 I have my own dataset and I want to perform a federated query in SPARQL. Here is the query: PREFIX : <http://myURIsNamespace#> PREFIX wd: <http://www.wikidata.org/entity/> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX p: <http://www.wikidata.org/prop/> PREFIX ps: <http://www.wikidata.org/prop/statement/> PREFIX pq: <http://www.wikidata.org/prop/qualifier/> PREFIX wdt: <http://www.wikidata.org/prop/direct/> select * where { ?bioentity :hasMutatedVersionOf ?gene . ?gene :partOf wd

T-SQL query performance puzzle: Why does using a variable make a difference?

て烟熏妆下的殇ゞ 提交于 2019-11-30 17:44:40
I'm trying to optimize a complex SQL query and getting wildly different results when I make seemingly inconsequential changes. For example, this takes 336 ms to run: Declare @InstanceID int set @InstanceID=1; With myResults as ( Select Row = Row_Number() Over (Order by sv.LastFirst), ContactID From DirectoryContactsByContact(1) sv Join ContainsTable(_s_Contacts, SearchText, 'john') fulltext on (fulltext.[Key]=ContactID) Where IsNull(sv.InstanceID,1) = @InstanceID and len(sv.LastFirst)>1 ) Select * From myResults Where Row between 1 and 20; If I replace the @InstanceID with a hard-coded number,

Query with many CASE statements - optimization

℡╲_俬逩灬. 提交于 2019-11-30 17:35:37
问题 I have one very dirty query that per sure can be optimized because there are so many CASE statements in it! SELECT (CASE pa.KplusTable_Id WHEN 1 THEN sp.sp_id WHEN 2 THEN fw.fw_id WHEN 3 THEN s.sw_Id WHEN 4 THEN id.ia_id END) as Deal_Id, max(CASE pa.KplusTable_Id WHEN 1 THEN sp.Trans_Id WHEN 2 THEN fw.Trans_Id WHEN 3 THEN s.Trans_Id WHEN 4 THEN id.Trans_Id END) as TransId_CurrentMax INTO #MaxRazlicitOdNull FROM #PotencijalniAktuelni pa LEFT JOIN kplus_sp sp (nolock) on sp.sp_id=pa.Deal_Id AND

Need help optimizing a lat/Lon geo search for mysql

為{幸葍}努か 提交于 2019-11-30 16:04:41
问题 I have a mysql (5.0.22) myisam table with roughly 300k records in it and I want to do a lat/lon distance search within a five mile radius. I have an index that covers the lat/lon fields and is fast (milisecond response) when I just select for lat/lon. But when I select for additional fields in the table is slows down horribly to 5-8 seconds. I'm using myisam to take advantage of fulltext search. The other indexes perform well (e.g. select * from Listing where slug = 'xxxxx'). How can I

Increment counter or insert row in one statement, in SQLite

浪子不回头ぞ 提交于 2019-11-30 14:41:49
问题 In SQLite, given this database schema CREATE TABLE observations ( src TEXT, dest TEXT, verb TEXT, occurrences INTEGER ); CREATE UNIQUE INDEX observations_index ON observations (src, dest, verb); whenever a new observation tuple (:src, :dest, :verb) comes in, I want to either increment the "occurrences" column for the existing row for that tuple, or add a new row with occurrences=1 if there isn't already one. In concrete pseudocode: if (SELECT COUNT(*) FROM observations WHERE src == :src AND