query-performance

Select distinct values from a table field

不羁的心 提交于 2019-11-28 15:33:47
I'm struggling getting my head around the Django's ORM. What I want to do is get a list of distinct values within a field on my table .... the equivalent of one of the following: SELECT DISTINCT myfieldname FROM mytable (or alternatively) SELECT myfieldname FROM mytable GROUP BY myfieldname I'd at least like to do it the Django way before resorting to raw sql. For example, with a table: id, street, city 1, Main Street, Hull 2, Other Street, Hull 3, Bibble Way, Leicester 4, Another Way, Leicester 5, High Street, Londidium I'd like to get: Hull, Leicester, Londidium. Say your model is 'Shop'

Deleting millions of rows in MySQL

做~自己de王妃 提交于 2019-11-28 03:22:38
I recently found and fixed a bug in a site I was working on that resulted in millions of duplicate rows of data in a table that will be quite large even without them (still in the millions). I can easily find these duplicate rows and can run a single delete query to kill them all. The problem is that trying to delete this many rows in one shot locks up the table for a long time, which I would like to avoid if possible. The only ways I can see to get rid of these rows, without taking down the site (by locking up the table) are: Write a script that will execute thousands of smaller delete

Why is there a HUGE performance difference between temp table and subselect

本秂侑毒 提交于 2019-11-28 03:20:26
This is a question about SQL Server 2008 R2 I'm not a DBA, by far. I'm a java developer, who has to write SQL from time to time. (mostly embedded in code). I want to know if I did something wrong here, and if so, what I can do to avoid it to happen again. Q1: SELECT something FROM (SELECT * FROM T1 WHERE condition1) JOIN ... Q1 features 14 joins Q2 is the same as Q1, with one exception. (SELECT * FROM T1 WHERE condition1) is executed before, and stored in a temp table. This is not a correlated sub-query. Q2: SELECT * INTO #tempTable FROM T1 WHERE condition1 SELECT something FROM #tempTable

How to FULL OUTER JOIN multiple tables in MySQL

纵饮孤独 提交于 2019-11-28 01:21:00
问题 I need to FULL OUTER JOIN multiple tables. I know how to FULL OUTER JOIN two tables from here. But I have several tables, and I can't apply it over them. How can I achieve it? My SQL code, below: INSERT INTO table ( customer_id ,g01 ,g02 ,g03 ,has_card ,activity ) SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity FROM s_geo_data sgd LEFT JOIN s_category sc ON sc.customer_id = sgd.customer_id UNION SELECT sgd.customer_id, sgd.g01,sgd.g02,sgd.g03,sc.value, a.activity FROM s

Conditional aggregation performance

我的未来我决定 提交于 2019-11-27 13:12:06
Let us have the following data IF OBJECT_ID('dbo.LogTable', 'U') IS NOT NULL DROP TABLE dbo.LogTable SELECT TOP 100000 DATEADD(day, ( ABS(CHECKSUM(NEWID())) % 65530 ), 0) datesent INTO [LogTable] FROM sys.sysobjects CROSS JOIN sys.all_columns I want to count the number of rows, the number of last year rows and the number of last ten years rows. This can be achieved using conditional aggregation query or using subqueries as follows -- conditional aggregation query SELECT COUNT(*) AS all_cnt, SUM(CASE WHEN datesent > DATEADD(year,-1,GETDATE()) THEN 1 ELSE 0 END) AS last_year_cnt, SUM(CASE WHEN

How do NULL values affect performance in a database search?

送分小仙女□ 提交于 2019-11-27 11:48:18
In our product we have a generic search engine, and trying to optimze the search performance. A lot of the tables used in the queries allow null values. Should we redesign our table to disallow null values for optimization or not? Our product runs on both Oracle and MS SQL Server . In Oracle , NULL values are not indexed, i. e. this query: SELECT * FROM table WHERE column IS NULL will always use full table scan since index doesn't cover the values you need. More than that, this query: SELECT column FROM table ORDER BY column will also use full table scan and sort for same reason. If your

Select distinct values from a table field

我是研究僧i 提交于 2019-11-27 09:17:10
问题 I'm struggling getting my head around the Django's ORM. What I want to do is get a list of distinct values within a field on my table .... the equivalent of one of the following: SELECT DISTINCT myfieldname FROM mytable (or alternatively) SELECT myfieldname FROM mytable GROUP BY myfieldname I'd at least like to do it the Django way before resorting to raw sql. For example, with a table: id, street, city 1, Main Street, Hull 2, Other Street, Hull 3, Bibble Way, Leicester 4, Another Way,

Increase Ms Access Insert Performance

▼魔方 西西 提交于 2019-11-27 03:44:28
问题 I am using MS Access 2010, split in front end / back end; on a network drive (WAN) with 16+ table with one table of users (1.3 Million) which is mostly used for user information and is not insert heavy and few other tables, which will receive upto 2000+ inserts daily . I have been able to optimize most of the read/select queries. Although 1 chunk of my code looks as below. This can be used for upto 2000 iterations daily. Do Until rec.EOF Dim vSomeId As Integer vSomeId = rec!SomeId 'StrSQL =

Conditional aggregation performance

ε祈祈猫儿з 提交于 2019-11-26 22:23:20
问题 Let us have the following data IF OBJECT_ID('dbo.LogTable', 'U') IS NOT NULL DROP TABLE dbo.LogTable SELECT TOP 100000 DATEADD(day, ( ABS(CHECKSUM(NEWID())) % 65530 ), 0) datesent INTO [LogTable] FROM sys.sysobjects CROSS JOIN sys.all_columns I want to count the number of rows, the number of last year rows and the number of last ten years rows. This can be achieved using conditional aggregation query or using subqueries as follows -- conditional aggregation query SELECT COUNT(*) AS all_cnt,

Query times out when executed from web, but super-fast when executed from SSMS

心已入冬 提交于 2019-11-26 21:55:42
I'm trying to debug the source of a SQL timeout in a web application that I maintain. I have the source code of the C# code behind, so I know exactly what code is running. I have debugged the application right down to the line that executes the SQL code that times out, and I watch the query running in SQL profiler. When this query executes from the web, it times out after 30 seconds. However, when I cut/paste the query exactly as presented in Profiler, and I put it into SSMS and run it, it returns almost instantly. I have traced the problem to ARITHABORT being set to OFF in the connection that