sql-optimization

How to get rid of the php wrapping on this sql query

北战南征 提交于 2019-12-13 02:43:53
问题 I have two tables. One is production table with posts and other is the collection of posts's views. Once a hour, all post views get grouped together and main production table gets updated. And post's views table gets truncated. My objective is to get this function into pure SQL, so I could run it inside database engine as scheduled event. SQL is my weakest point, and I can make it work with SELECT being inside one UPDATE. But if there are two UPDATE queries, then I need some sort of variable.

Optimise multiple OR query

只谈情不闲聊 提交于 2019-12-12 19:16:49
问题 I have a Database table where I need to validate if a user has entered in the same or partly the same information. Here is what I'm thinking The db layout rec_id (pk), user_id, name, phone, address_1, address_2, zip, company, co_phone, co_address_1, co_address_2, co_zip, billing, bi_phone, bi_address_1, bi_address_2, bi_zip The Query SELECT rec_id FROM tbl_name WHERE user_id = '123456789' OR '1112223333' IN (phone, co_phone, bi_phone) OR 'John Doe' IN (name, business, billing) OR '12345' IN

Can JDBC client execute SET ARITHABORT ON on Microsoft SQL Server 2012?

ぃ、小莉子 提交于 2019-12-11 09:14:13
问题 Can I set Microsoft SQL Server 2012 options such as ARITHABORT programmatically from a (remote) Java client that uses Microsoft JDBC Driver 4.0? I've noticed that statement.execute("SET ARITHABORT ON") returns false and the expected effect seems missing, hence the question. 回答1: Can I set Microsoft SQL Server 2012 options such as ARITHABORT programmatically from a (remote) Java client that uses Microsoft JDBC Driver 4.0? It certainly appears so. The code package com.example.sqlservertest;

SQL Server : Issue in exists query replace with joins

北城以北 提交于 2019-12-11 07:28:42
问题 I am using the following code to fetch the report. DECLARE @CountryID smallint = 100 DECLARE @UTCTDIM smallint = 330 DECLARE @MarketProfileID smallint = 2 DECLARE @StartDate datetime = '2017-01-01' DECLARE @EndDate datetime = '2017-01-03' DECLARE @CustomerTypeID smallint = 1 SET NOCOUNT ON DECLARE @AirportList TABLE (ID bigint) INSERT INTO @AirportList (ID) SELECT DISTINCT Airport.ID FROM Airport INNER JOIN CityList ON Airport.CityID = CityList.ID INNER JOIN CountryList ON CityList.CountryID

Are Columns Not Selected in SQL Views Executed?

你。 提交于 2019-12-11 03:54:49
问题 I wasn't able to come up with the right keywords to search for the answer for this, so apologies if it was answered already. Consider the following SQL view: CREATE VIEW View1 AS SELECT Column1 ,Column2 ,(SELECT SUM(Column3) FROM Table2 WHERE Table2.ID = Table1.ID) -- Subquery FROM Table1 If I run the following query, will the subquery be executed or does SQL Server optimise the query? SELECT Column1 FROM View1 I'm looking at this from a performance point of view, say, if the view has quite a

SQL Server query optimization?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 15:39:47
问题 I have the following query: SELECT tl.*, d.* FROM TrackerLocations AS tl inner join Trackers t on tl.TrackerId = t.TrackerId inner join Devices d on t.UserId = d.UserId WHERE tl.ReceivedTime = (SELECT MAX(tl2.ReceivedTime) FROM TrackerLocations tl2 WHERE tl2.TrackerId = tl.TrackerId) and tl.ReceivedTime >= DATEADD (MINUTE,-2,GETUTCDATE()) and d.OSType <> 3 and d.Notify = 1 ...and for my surprise, it does not return the results in an acceptable time. First times I runned it in production

SQL Server query optimization?

大兔子大兔子 提交于 2019-12-06 00:35:45
I have the following query: SELECT tl.*, d.* FROM TrackerLocations AS tl inner join Trackers t on tl.TrackerId = t.TrackerId inner join Devices d on t.UserId = d.UserId WHERE tl.ReceivedTime = (SELECT MAX(tl2.ReceivedTime) FROM TrackerLocations tl2 WHERE tl2.TrackerId = tl.TrackerId) and tl.ReceivedTime >= DATEADD (MINUTE,-2,GETUTCDATE()) and d.OSType <> 3 and d.Notify = 1 ...and for my surprise, it does not return the results in an acceptable time. First times I runned it in production environment, was like 3 seconds of execution. Now, it runs until timeout (inside C# application, 30 seconds)

How do I select a random record efficiently in MySQL?

倖福魔咒の 提交于 2019-12-04 14:21:46
问题 mysql> EXPLAIN SELECT * FROM urls ORDER BY RAND() LIMIT 1; +----+-------------+-------+------+---------------+------+---------+------+-------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+-------+---------------------------------+ | 1 | SIMPLE | urls | ALL | NULL | NULL | NULL | NULL | 62228 | Using temporary; Using filesort | +----+---------

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

折月煮酒 提交于 2019-12-03 14:04:21
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 DEFAULT NULL, `updated_at` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8

How do I select a random record efficiently in MySQL?

会有一股神秘感。 提交于 2019-12-03 07:58:56
mysql> EXPLAIN SELECT * FROM urls ORDER BY RAND() LIMIT 1; +----+-------------+-------+------+---------------+------+---------+------+-------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+-------+---------------------------------+ | 1 | SIMPLE | urls | ALL | NULL | NULL | NULL | NULL | 62228 | Using temporary; Using filesort | +----+-------------+-------+------+---------------+------+---------+------+-------+---------------------------------+