query-optimization

Using T-SQL EXCEPT with DELETE / Optimizing a query

安稳与你 提交于 2020-01-17 04:44:26
问题 The following code removes records of tasks related to inactive projects from the table. delete from [Deliverables] where [Deliverables].[ProjectID] not in ( select [ProjectID] from [ActiveProjects] ) I've read somewhere that using NOT IN with subquery that returns a lot of values is not really the most efficient thing to do and it's better to use EXCEPT clause. However, when I try to use the following code, I get an error (Incorrect syntax near the keyword 'except'.) delete from

jQuery - how to calculate a product price from a table of product names, quantity ranges and price ranges

﹥>﹥吖頭↗ 提交于 2020-01-16 05:06:07
问题 I have 3 products A, B, C Then I have 2 addons for those products, : 1, 2 Then I have table of price ranges vs quantity ranges. See below PRODUCT vs QUANTITY PRICES +---------------+--------+----------+ | Product | Qty | Price | +---------------+--------+----------+ | A | 0-10 | $2 | | A | 10-20 | $1 | +---------------+--------+----------+ | B | 0-10 | $3 | | B | 10-20 | $2 | +---------------+--------+----------+ | C | 0-10 | $4 | | C | 10-20 | $3 | +---------------+--------+----------+

Calculating how many Working Days between 2 Dates - T-SQL?

自闭症网瘾萝莉.ら 提交于 2020-01-16 00:34:10
问题 I realise different solutions will have different variations of what "Working Days" means but in my case I mean Monday to Friday inclusive. Basically I have Created a function to do the calculation for me and my current solution works. My concern (and reason for asking this question) is that I am worried that this is a bad way of achieving this because the function is being called with a very high frequency. In the last 3 months it has been called 12 million times on a production system, with

Why is the leftmost subset of a key not being used to optimise this ORDER BY?

橙三吉。 提交于 2020-01-15 11:58:09
问题 Server version: [root@cat best]# /usr/libexec/mysqld --version /usr/libexec/mysqld Ver 5.1.47 for redhat-linux-gnu on i386 (Source distribution) Schema: CREATE TABLE `Log` ( `EntryId` INT UNSIGNED NOT NULL AUTO_INCREMENT, `EntryTime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(), `Severity` ENUM( 'LOG_LEVEL_CRITICAL', 'LOG_LEVEL_ERROR', 'LOG_LEVEL_WARNING', 'LOG_LEVEL_NOTICE', 'LOG_LEVEL_INFO', 'LOG_LEVEL_DEBUG' ) NOT NULL, `User` TEXT, `Text` TEXT NOT NULL, PRIMARY KEY(`EntryId`), KEY

MySql Query very slow

淺唱寂寞╮ 提交于 2020-01-14 14:19:09
问题 I run the following query on my database : SELECT e.id_dernier_fichier FROM Enfants e JOIN FichiersEnfants f ON e.id_dernier_fichier = f.id_fichier_enfant And the query runs fine. If I modifiy the query like this : SELECT e.codega FROM Enfants e JOIN FichiersEnfants f ON e.id_dernier_fichier = f.id_fichier_enfant The query becomes very slow ! The problem is I want to select many columns in table e and f, and the query can take up to 1 minute ! I tried different modifications but nothing works

How to optimize huge query with repeated subqueries

时光毁灭记忆、已成空白 提交于 2020-01-13 19:30:10
问题 I have the following huge query that contains repeated subqueries , It looks really inefficient to me. How can i optimize it ? SELECT T2.date1, T2.date2, T2.period, T1.market, T1.ticker, 0 AS scenario FROM (SELECT DISTINCT Q.market AS market, Q.ticker AS ticker FROM portfolio.scenario S RIGHT JOIN portfolio.quote Q ON S.series = (SELECT S.series FROM scenario S WHERE S.date1 >= '2009-09-01' AND S.date2 <= '2010-07-01' AND S.period = 'QUARTER' ORDER BY S.date2 LIMIT 1) AND Q.market = S.market

Does MySQL use existing indexes on creating new indexes?

我的梦境 提交于 2020-01-13 19:15:07
问题 I have a large table with millions of records. Table `price` ------------ id product site value The table is brand new, and there are no indexes created. I then issued a request for new index creation with the following query: CREATE INDEX ix_price_site_product_value_id ON price (site, product, value, id); This took long long time, last time I was checking ran for 5000+ seconds, because of the machine. I am wondering if I issue another index creation, will it use the existing index in the

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

一个人想着一个人 提交于 2020-01-13 16:29:52
问题 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 | +-------+-------------------+---------------------+-------------+---------

Using OR on WHERE statement in MySql causes slow excecution

依然范特西╮ 提交于 2020-01-11 12:09:26
问题 The following query takes mysql to execute almost 7 times longer than implementing the same using two separate queries, and avoiding OR on the WHERE statement. I prefer using a single query as I can sort and group everything. Here is the problematic query: EXPLAIN SELECT * FROM `posts` LEFT JOIN `teams_users` ON (teams_users.team_id=posts.team_id AND teams_users.user_id='7135') WHERE (teams_users.status='1' OR posts.user_id='7135'); Result: id select_type table type possible_keys key key_len

Optimization of greater than operator

ぐ巨炮叔叔 提交于 2020-01-11 11:22:28
问题 credit table and validtransaction table have million record data from year 2008 onwards. We are doing a migration. So I need to find out the credittypeids which are no longer in use after 2017 (periodseq=1055), so that these need not be migrated. This is the query and the >= part is resulting in huge cost. Please suggest an alternative. SELECT CREDITTYPEID FROM CREDITTYPE ct WHERE NOT EXISTS (SELECT 1 FROM CREDIT C WHERE C.PERIODSEQ>=1055 AND C.CREDITTYPEID=CT.CREDITTYPEID ); 回答1: The