sql-optimization

How to optimise this MySQL query? Millions of Rows

筅森魡賤 提交于 2021-02-07 04:42:46
问题 I have the following query: SELECT analytics.source AS referrer, COUNT(analytics.id) AS frequency, SUM(IF(transactions.status = 'COMPLETED', 1, 0)) AS sales FROM analytics LEFT JOIN transactions ON analytics.id = transactions.analytics WHERE analytics.user_id = 52094 GROUP BY analytics.source ORDER BY frequency DESC LIMIT 10 The analytics table has 60M rows and the transactions table has 3M rows. When I run an EXPLAIN on this query, I get: +------+--------------+-----------------+--------+---

How to delete large data of table in SQL without log?

纵然是瞬间 提交于 2021-01-29 11:43:32
问题 I have a large data table. There are 10 million records in this table. What is the best way for this query Delete LargeTable where readTime < dateadd(MONTH,-7,GETDATE()) 回答1: If you are Deleting All the rows in that table the simplest option is to Truncate table, something like TRUNCATE TABLE LargeTable GO Truncate table will simply empty the table, you cannot use WHERE clause to limit the rows being deleted and no triggers will be fired. On the other hand if you are deleting more than 80-90

Why SQL Server execution plan depends on comparison order

馋奶兔 提交于 2020-05-27 02:33:56
问题 I was optimising a query on SQL Server and ran into something I was not expecting. There is a table tblEvent in the database, among other columns it has IntegrationEventStateId and ModifiedDateUtc . There is an index by these columns: create index IX_tblEvent_IntegrationEventStateId_ModifiedDateUtc on dbo.tblEvent ( IntegrationEventStateId, ModifiedDateUtc ) When I execute the following statement: select * from dbo.tblEvent e where e.IntegrationEventStateId = 1 or e.IntegrationEventStateId =

SP execution time is extremely slow

别说谁变了你拦得住时间么 提交于 2020-01-06 08:11:56
问题 I created a stored procedure that calculates a financial spreading based on a linear self adjusting rule and it takes more than 2 minutes to finish the calculations. The final value goes through multiple iterations in order to adjust and enhance it till finding the optimal optimized final value. The parameters are the following: @input1 = 100000 @input2 = 40 @input3 = 106833 BEGIN DECLARE @X decimal(22,6) = 0 DECLARE @Y decimal(22,6) = 0.001 DECLARE @Z decimal(22,6) DECLARE @r decimal(22,6)

Not efficient execution plan taken by MySQL InnoDB

邮差的信 提交于 2020-01-05 10:08:14
问题 I have trouble to optimize a request with the MySQL InnoDB optimizer. The following query (query 1) runs efficiently: explain select * from ah_problems where rnid in (6022342, 6256614, 5842714, 6302489) and fieldid in (5,6); and the plan (plan 1) is as follows: id select_type table type possible_keys key key_len ref rows Extra = ====== =========== ===== =============================== ============= ======= === ==== ===== 1 SIMPLE ah_problems range CONSTRAINTFIELDID,RNID__FIELDID RNID__FIELDID

can it be executed faster with big amount of data [MySQL]

做~自己de王妃 提交于 2019-12-25 11:48:44
问题 is there any way how to optimize next query: EXPLAIN EXTENDED SELECT keyword_id, ck.keyword, COUNT( article_id ) AS cnt FROM career_article_keyword LEFT JOIN career_keywords ck USING ( keyword_id ) WHERE keyword_id IN ( SELECT keyword_id FROM career_article_keyword LEFT JOIN career_keywords ck USING ( keyword_id ) WHERE article_id IN ( SELECT article_id FROM career_article_keyword WHERE keyword_id =9 ) AND keyword_id <>9 ) GROUP BY keyword_id ORDER BY cnt DESC The main task here if I have

Sql Server: Selective XML Index not being efficiently used

为君一笑 提交于 2019-12-23 10:28:18
问题 I'm exploring ways of improving the performance of an application which I can only affect on the database level to a limited degree. The SQL Server version is 2012 SP2 and the table and view structure in question is (I cannot really affect this + note that the xml document may have several hundred elements in total): CREATE TABLE Orders( id nvarchar(64) NOT NULL, xmldoc xml NULL, CONSTRAINT PK_Order_id PRIMARY KEY CLUSTERED (id) ); CREATE VIEW V_Orders as SELECT a.id, a.xmldoc ,a.xmldoc.value

SQL: How to select one record per day, assuming that each day contain more than 1 value MySQL

北战南征 提交于 2019-12-21 04:26:14
问题 I want to select records from '2013-04-01 00:00:00' to 'today' but, each day has lot of value, because they are saving each 15 minutes a value, so I want only the first or last value from each day. Table schema: CREATE TABLE IF NOT EXISTS `value_magnitudes` ( `id` int(11) NOT NULL AUTO_INCREMENT, `value` float DEFAULT NULL, `magnitude_id` int(11) DEFAULT NULL, `sdi_belongs_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `reading_date` datetime DEFAULT NULL, `created_at` datetime

How to delete large data of table in SQL without log?

我的梦境 提交于 2019-12-17 03:02:10
问题 I have a large data table. There are 10 million records in this table. What is the best way for this query Delete LargeTable where readTime < dateadd(MONTH,-7,GETDATE()) 回答1: If you are Deleting All the rows in that table the simplest option is to Truncate table, something like TRUNCATE TABLE LargeTable GO Truncate table will simply empty the table, you cannot use WHERE clause to limit the rows being deleted and no triggers will be fired. On the other hand if you are deleting more than 80-90

Need to see if a range of dates overlaps another range of dates in sql

99封情书 提交于 2019-12-14 03:52:00
问题 I have a table which stores bookings of rooms, the schema is: ID | ROOM_ID | CHECK_IN_DATE | CHECK_OUT_DATE | USER_ID I need to run a search query for rooms which are available/unavailable between a set range of dates. Also keep in mind that there exists another table which holds dates when the room is prebooked and its in the format: ROOM_ID | DATE SO I need to run a query which looks for rooms available within a set range, How would I formulate the query? I'm using MySQL here. ---edit---