query-optimization

Optimization of greater than operator

感情迁移 提交于 2020-01-11 11:21:51
问题 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

Create a ROLLING sum over a period of time in mysql

末鹿安然 提交于 2020-01-11 10:48:12
问题 I have a table with columns date and time_spent . I want to find for each date D the sum of the values of 'time_spent' for the period of time : (D-7 - D), ie. past week + current day. I can't figure out a way to do this, as I can only find examples for a total sum and not a sum over a variable period of time. Here is a dataset example : CREATE TABLE rolling_total ( date date, time_spent int ); INSERT INTO rolling_total VALUES ('2013-09-01','2'), ('2013-09-02','1'), ('2013-09-03','3'), ('2013

Understanding spark physical plan

怎甘沉沦 提交于 2020-01-09 09:01:31
问题 I'm trying to understand physical plans on spark but I'm not understanding some parts because they seem different from traditional rdbms. For example, in this plan below, it's a plan about a query over a hive table. The query is this: select l_returnflag, l_linestatus, sum(l_quantity) as sum_qty, sum(l_extendedprice) as sum_base_price, sum(l_extendedprice * (1 - l_discount)) as sum_disc_price, sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) as sum_charge, avg(l_quantity) as avg_qty, avg

Sql NOT IN optimization

怎甘沉沦 提交于 2020-01-07 05:03:32
问题 I'm having trouble optimizing a query. Here are two example tables I am working with: Table 1: UID A B Table 2: UID Parent A 2 B 2 C 3 D 2 E 3 F 2 Here is what I am doing now: Select Table1.UID FROM Table1 R INNER JOIN Table2 T ON R.UID = T.UID INNER JOIN Table2 E ON T.PARENT = E.PARENT AND E.UID NOT IN (SELECT UID FROM Table1) I'm trying to avoid using the NOT IN clause because of obvious hindrances in performance for large numbers of records. I know the typical ways to avoid NOT IN clauses

SQL - How to find optimal performance numbers for query

寵の児 提交于 2020-01-07 04:20:35
问题 First time here so forgive me for any faux pas. I have a question about the limitation of SQL as I am new to the code, and what I need I believe to be rather complex. Is it possible to automate finding the optimal data for a specific query. For example, say I have the following columns: 1) Vehicle type (Text) e.g. car,bike,bus 2) Number of passengers (Numeric) e.g. 0-7 3) Was in an accident (Boolean) e.g. t or f From here, I would like to get percentages. So if I were to select only cars with

Speeding up insert query over 2 databases

爷,独闯天下 提交于 2020-01-07 02:23:48
问题 I have this query below which I am getting certain columns from 1 database and I am then inserting them into another table in another database. I will then Delete the table I am copying from. At the moment it takes 5 minutes and 36 seconds to copy a bit over 5300 records. Is there any way I can improve the speed? Declare @cursor cursor, @Firstname nchar(50), @MiddleInitial nchar(5), @Surname nchar(50), @EmailAddress nchar(100), @DOB nchar(8), @Sex char(1), @altEmail nchar(100) set @cursor =

Speeding up insert query over 2 databases

依然范特西╮ 提交于 2020-01-07 02:23:21
问题 I have this query below which I am getting certain columns from 1 database and I am then inserting them into another table in another database. I will then Delete the table I am copying from. At the moment it takes 5 minutes and 36 seconds to copy a bit over 5300 records. Is there any way I can improve the speed? Declare @cursor cursor, @Firstname nchar(50), @MiddleInitial nchar(5), @Surname nchar(50), @EmailAddress nchar(100), @DOB nchar(8), @Sex char(1), @altEmail nchar(100) set @cursor =

what can I do to optimize the following function or some other thing to reduce memory consumption?

别说谁变了你拦得住时间么 提交于 2020-01-06 09:06:05
问题 I am working on business directory sort of thing, and need to show recursive parents of categories at category listing. I am using following function for that: public function get_recursive_parents($category_id){ $categories = array(); $res = $this->db->from('categories')->where('cat_id',$category_id)->get()->row_array(); $cat_id = $res['parent_id']; $categories[] = $res; while($cat_id){ $res = $this->db->from('categories')->where('cat_id',$cat_id)->get()->row_array(); $categories[] = $res;

Query equivalence with DISTINCT

◇◆丶佛笑我妖孽 提交于 2020-01-05 07:32:11
问题 Let us have a simple table order(id: int, category: int, order_date: int) created using the following script IF OBJECT_ID('dbo.orders', 'U') IS NOT NULL DROP TABLE dbo.orders SELECT TOP 1000000 NEWID() id, ABS(CHECKSUM(NEWID())) % 100 category, ABS(CHECKSUM(NEWID())) % 10000 order_date INTO orders FROM sys.sysobjects CROSS JOIN sys.all_columns Now, I have two equivalent queries (at least I believe that they are equivalent): -- Q1 select distinct o1.category, (select count(*) from orders o2

Slow ORDER BY in large table

被刻印的时光 ゝ 提交于 2020-01-05 04:31:07
问题 I have problem with ORDER BY clause. When I remove ORDER BY in following query, query is finished in 0.004 seconds. If I keep it, query is running very slow = 56 seconds. It is because MySQL is first grabbing all 500 000 records, then sorting and finally returing only first 18 records. How can I solve this big table ordering problem ? Thanks. Explain: http://img444.imageshack.us/img444/9440/explain.png SQL Query: SELECT `_sd`.`sazbaDPHId`, `_sd`.`sazbaDPH`, `_sd`.`sazbaDPHProcent`, `_zk`.