query-optimization

How to properly index a many-many association table?

天大地大妈咪最大 提交于 2019-12-02 20:35:38
In a typical many-many arrangement like this... Movies Actors Movies_Actors ------ ------ ------------- movie_ID actor_ID FK_movie_ID title name FK_actor_ID ... how should the association table ( 'Movies_Actors' ) be indexed for optimal read speed? I usually see this done only with the composite primary key in the association table, like so: CREATE TABLE Movies_Actors ( FK_movie_ID INTEGER, FK_actor_ID INTEGER, PRIMARY KEY (FK_movie_ID, FK_actor_ID) ) However, this seems like the index will only be useful when searching for both movie_ID and actor_ID (although I'm not certain on whether a

MySQL Enhancing Performance without Cache

点点圈 提交于 2019-12-02 18:17:09
问题 I am using MySQL version 5.5.14 to run the following query 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(

How to compare two queries?

孤街醉人 提交于 2019-12-02 17:12:28
How can I compare two queries X and Y and say that X is better than Y, when they both take almost the same time in small cases scenarios? The problem is that I have two queries that are supposed to run on a very big database, so run and evaluate is not quite an option. Therefore, we created a small database to perform some tests. Evaluating which query is better is a problem, since on our test base, they run in almost the same time (about 5 minutes). Besides the time taken to return, what is another way to measure how good a query is? SET STATISTICS IO ON SET STATISTICS TIME ON Run the queries

Optimizing a query returning a lot of records, a way to avoid hundreds of join. Is it a smart solution?

冷暖自知 提交于 2019-12-02 17:04:37
问题 I am not so int SQL and I have the following doubt about how to optimize a query. I am using MySql I have this DB schema: And this is the query that returns the last price (the last date into the Market_Commodity_Price_Series table) of a specific commodity into a specific market. It contains a lot of join to retrieve all the related information: SELECT MCPS.id AS series_id, MD_CD.market_details_id AS market_id, MD_CD.commodity_details_id AS commodity_id, MD.market_name AS market_name, MCPS

Downsides to “WITH SCHEMABINDING” in SQL Server?

我们两清 提交于 2019-12-02 16:29:42
I have a database with hundreds of awkwardly named tables in it (CG001T, GH066L, etc), and I have views on every one with its "friendly" name (the view "CUSTOMERS" is "SELECT * FROM GG120T", for example). I want to add "WITH SCHEMABINDING" to my views so that I can have some of the advantages associated with it, like being able to index the view, since a handful of views have computed columns that are expensive to compute on the fly. Are there downsides to SCHEMABINDING these views? I've found some articles that vaguely allude to the downsides, but never go into them in detail. I know that

Handling large databases

筅森魡賤 提交于 2019-12-02 16:24:32
I have been working in a web project(asp.net) for around six months. The final product is about to go live. The project uses SQL Server as the database. We have done performance testing with some large volumes of data, results show that performance degrades when data becomes too large, say 2 million rows (timeout issues, delayed reponses, etc). At first we were using fully normailized database, but now we made it partially normalized due to performance issues (to reduce joins). First of all, is it the right decision? Plus what are the possible solutions when data size becomes very large, as

What is the difference between Seq Scan and Bitmap heap scan in postgres?

本秂侑毒 提交于 2019-12-02 16:07:30
In output of explain command I found two terms 'Seq Scan' and 'Bitmap heap Scan'. Can somebody tell me what is the difference between these two types of scan? (I am using PostgreSql) http://www.postgresql.org/docs/8.2/static/using-explain.html Basically, a sequential scan is going to the actual rows, and start reading from row 1, and continue until the query is satisfied (this may not be the entire table, e.g., in the case of limit) Bitmap heap scan means that PostgreSQL has found a small subset of rows to fetch (e.g., from an index), and is going to fetch only those rows. This will of course

How do I implement threaded comments?

耗尽温柔 提交于 2019-12-02 14:18:42
I am developing a web application that can support threaded comments. I need the ability to rearrange the comments based on the number of votes received. (Identical to how threaded comments work in reddit ) I would love to hear the inputs from the SO community on how to do it. How should I design the comments table? Here is the structure I am using now: Comment id parent_post parent_comment author points What changes should be done to this structure? How should I get the details from this table to display them in the correct manner? (Implementation in any language is welcome. I just want to

Improving OFFSET performance in PostgreSQL

别来无恙 提交于 2019-12-02 14:03:25
I have a table I'm doing an ORDER BY on before a LIMIT and OFFSET in order to paginate. Adding an index on the ORDER BY column makes a massive difference to performance (when used in combination with a small LIMIT). On a 500,000 row table, I saw a 10,000x improvement adding the index, as long as there was a small LIMIT. However, the index has no impact for high OFFSETs (i.e. later pages in my pagination). This is understandable: a b-tree index makes it easy to iterate in order from the beginning but not to find the nth item. It seems that what would help is a counted b-tree index , but I'm not

Speed up plpgsql that counts doc types in a loop?

瘦欲@ 提交于 2019-12-02 13:08:56
问题 Is there a way to speed up our plpgsql function that counts certain types of docs all in one query which is executed in a loop? ALL in one query? validador := (select count(id_doc) from webdte.doc_tip_cifra where id_doc = id_documento and id_tipo_cifra = 901); validador2 := (select count(id_doc) from webdte.doc_tip_cifra where id_doc = id_documento and id_tipo_cifra = 902); validador3 := (select count(id_doc) from webdte.doc_tip_cifra where id_doc = id_documento and id_tipo_cifra = 905);