query-optimization

MySQL select with a count query on another table

馋奶兔 提交于 2019-12-06 07:28:34
I have simple article application with three tables: article id, title, body, user_id comment id, article_id, user_id, body user id, username On the landing page, I want to show the latest article titles with the author name and total numbers of comments of the article. The main problem is how to get total numbers of comments of the article,I did not get it right. I should get the following output: title username total_comments article 2 user2 0 article 1 user1 2 In my real application, I added a column in article table for total number of comments to the article. this column is updated when a

Cacheable(), FetchMany() and ToFuture() in same NHibernate Linq query

孤者浪人 提交于 2019-12-06 06:21:49
Having a situation similar to the following example: 1 parent entity Employee having 2 child collections: Addresses and Phones I need to retrieve in a single roundtrip all employees with their Addresses and Phones initialized and also cache the query that achieves that in level 2 cache using Cacheable() . Using: var baseQuery = session .Query<Employee>() .Cacheable(); baseQuery .FetchMany(e => e.Addresses) .ToFuture(); var list = baseQuery .FetchMany(e => e.Phones) .ToFuture() .ToList(); should work, but I get the following exception: NHibernate.PropertyAccessException Message: Exception

MySQL index for MIN and MAX

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 05:34:15
问题 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

Which is the best choice in delete-insert vs if-update else-insert?

江枫思渺然 提交于 2019-12-06 01:43:06
问题 Update : My bad...I have an primary key on those tables..I meant no further indexing currently on the tables. We might have it in the future after seeing the performance and since we have too many filters on the data in retrieving data it did not show much improvement on indexing last time we ran database tuning. I have a 4 huge tables over millions of records. Now there is stored procedure which is called frequently and updates these table. Here is the scenario - Now if entry exists for

MySQL explain filtered column jumping 4,100 with index

半世苍凉 提交于 2019-12-06 01:25:11
问题 My Query: EXPLAIN EXTENDED SELECT `artwork`.`id` , `artwork`.`added` FROM `artwork` ORDER BY `artwork`.`added` DESC LIMIT 0 , 6 When I added an index on "added" to avoid using filesort and use index instead the output of explained went from id select_type table type possible_keys key key_len ref rows filtered Extra 1 SIMPLE artwork ALL NULL NULL NULL NULL 302 100.00 Using filesort to id select_type table type possible_keys key key_len ref rows filtered Extra 1 SIMPLE artwork index NULL added

Very slow subqueries when using “NOT IN”

隐身守侯 提交于 2019-12-05 23:13:49
问题 I'm working on generating reports for data contained within a large pre-existing Access database (~500 mb after compact & repair), and I'm having trouble with a slow subquery. The database has a big table which contains a record of every customer purchase. Here's a simple query which finds customers who have bought a blue widget. It completes within a few seconds and returns about ten thousand records. SELECT DISTINCT CustomerId FROM ProductSales WHERE Product = 'BLUE' Here's a query which

Optimization of SPARQL query. [ Estimated execution time exceeds the limit of 1500 (sec) ]

两盒软妹~` 提交于 2019-12-05 22:49:34
I am trying to run this query on http://dbpedia.org/sparql but I get an error that my query is too expensive. When I run the query trough http://dbpedia.org/snorql/ I get: The estimated execution time 25012730 (sec) exceeds the limit of 1500 (sec) ... When running the query through my python script using SPARQLWrapper I simply get an HTTP 500. I figure I need to do something to optimize my SPARQL query. I need the data for iterating over educational institutions and importing it in to a local database, maybe I am using SPARQL wrong and should do this in a fundamentally different way. Hope

How to obtain the most recent row per type and perform calculations, depending on the row type?

谁说胖子不能爱 提交于 2019-12-05 21:30:45
I need some help writing/optimizing a query to retrieve the latest version of each row by type and performing some calculations depending on the type. I think would be best if I illustrate it with an example. Given the following dataset: +-------+-------------------+---------------------+-------------+---------------------+--------+----------+ | id | event_type | event_timestamp | message_id | sent_at | status | rate | +-------+-------------------+---------------------+-------------+---------------------+--------+----------+ | 1 | create | 2016-11-25 09:17:48 | 1 | 2016-11-25 09:17:48 | 0 | 0

Suitable indexes for sorting in ranking functions

我们两清 提交于 2019-12-05 17:29:41
I have a table which keeps parent-child-relations between items. Those can be changed over time, and it is necessary to keep a complete history so that I can query how the relations were at any time. The table is something like this (I removed some columns and the primary key etc. to reduce noise): CREATE TABLE [tblRelation]( [dtCreated] [datetime] NOT NULL, [uidNode] [uniqueidentifier] NOT NULL, [uidParentNode] [uniqueidentifier] NOT NULL ) My query to get the relations at a specific time is like this (assume @dt is a datetime with the desired date): SELECT * FROM ( SELECT ROW_NUMBER() OVER

Foreign key optimization in SQLite

泄露秘密 提交于 2019-12-05 10:27:02
问题 I know that SQLite does not enforce foreign keys natively, but that's not my primary concern. The question is: If I declare CREATE TABLE invoice ( invoiceID INTEGER PRIMARY KEY, clientID INTEGER REFERENCES client(clientID), ... ) will sqlite at least use the information that clientID is a foreign key to optimize queries and automatically index invoice.clientID, or is this constraint a real no-op? 回答1: Even if it is not actually a no-op (a data structure describing the constraint is added to