sql-order-by

Why is this INNER JOIN/ORDER BY mysql query so slow?

一世执手 提交于 2019-12-11 13:07:55
问题 I have very big database of customers. This query was ok before I added ORDER BY . How can I optimize my query speed? $sql = "SELECT * FROM customers LEFT JOIN ids ON customer_ids.customer_id = customers.customer_id AND ids.type = '10' ORDER BY customers.name LIMIT 10"; ids.type and customers.name are my indexes Explain query id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE customers ALL NULL NULL NULL NULL 955 Using temporary; Using filesort 1 SIMPLE ids ALL type

DB2 - Is there a simple way to get “previous valid value”?

断了今生、忘了曾经 提交于 2019-12-11 12:58:23
问题 I'm not sure if it is appropriate to call the problem as "previous valid value". The thing is like below: I have a table "A": create table A ( name varchar(16), performanceDate date, value int ); Both name and PerformanceDate are primary key. There is a process that will insert data for each user every day. So, the data will look something like below: select * from A; |------+-----------------+-------| | name | performanceDate | value | |------+-----------------+-------| | Joe | 2012-05-18 |

How order by clause works in mysql, ordering shows wierd behaviour

↘锁芯ラ 提交于 2019-12-11 12:31:34
问题 I have a simple query SELECT * FROM table_name order by non_unique_column LIMIT 0,50 This gives me list of records in some order. But when I am removing * and putting some column names from the table, then it is changing the list order. For some combination of columns in select clause it is giving different order. I am not able to find, what pattern of column names in select clause gives different ordering. So I am asking does any one had this kind of problem? or can any one suggest how the

MySQL: Selecting rows ordered by word count

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 12:19:57
问题 How can I order my query by word count? Is it possible? I have some rows in table, with text fields. I want to order them by word count of these text fields. Second problem is, that I need to select only these rows, which have for example minimum 10 words, or maximum 20. 回答1: Well, this will not perform very well since string calculations need to be performed for all rows: You can count number of words in a MySQL column like so: SELECT SUM( LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1)

Pyspark dafaframe OrderBy list of columns [duplicate]

狂风中的少年 提交于 2019-12-11 12:05:00
问题 This question already has an answer here : How to select and order multiple columns in a Pyspark Dataframe after a join (1 answer) Closed last year . I am trying to use OrderBy function in pyspark dataframe before I write into csv but I am not sure to use OrderBy functions if I have a list of columns. Code: Cols = ['col1','col2','col3'] df = df.OrderBy(cols,ascending=False) 回答1: As per docstring / signature: Signature: df.orderBy(*cols, **kwargs) Docstring: Returns a new :class:`DataFrame`

Get one record per unique column value

元气小坏坏 提交于 2019-12-11 11:16:52
问题 I have a table that list system licences, multiple licences for each system (the expired ones and existing ones). I've only posted two columns in this question as they're the only important ones. | id | systemid | | 1 | 1 | | 2 | 1 | | 3 | 2 | | 4 | 2 | | 5 | 3 | | 6 | 3 | I need to get the rows with the id of 2, 4 and 6. I need to collect 1 record for each systemid and it has to be the earliest (youngest) record, so in this case, the record with the highest id. I've been exploring GROUP BY ,

Deterministic sort order for window functions

别说谁变了你拦得住时间么 提交于 2019-12-11 11:08:42
问题 I've a status table and I want to fetch the latest details. Slno | ID | Status | date 1 | 1 | Pass | 15-06-2015 11:11:00 - this is inserted first 2 | 1 | Fail | 15-06-2015 11:11:00 - this is inserted second 3 | 2 | Fail | 15-06-2015 12:11:11 - this is inserted first 4 | 2 | Pass | 15-06-2015 12:11:11 - this is inserted second I use a window function with partition by ID order by date desc to fetch the first value. Excepted Output : 2 | 1 | Fail | 15-06-2015 11:11:00 - this is inserted second

Sorting Dynamic LINQ By Collection Property

柔情痞子 提交于 2019-12-11 10:37:23
问题 I am using System.Linq.Dynamic and have the following simplified structure (model): Parent -> Children (Collection of type Child) Child has property SortingOrder I would like retrieve list of Parents include Children and order by Sorting Order. I can do that with LINQ / C# code Parents.OrderBy(x=>x.Children.OrderBy(z=>z.SortingOrder).Select(z=>z.SortingOrder).FirstOrDefault()) It works fine with EF (6.0) but problem is with all dynamic examples using string I cannot find solution to create

TSQL - Get the difference between each row value

爷,独闯天下 提交于 2019-12-11 10:32:15
问题 I am storing data in the following table - [dbo].[readings] [id] [int] IDENTITY(1,1) NOT NULL, [device_id] [int] NOT NULL, [time] [datetime] NOT NULL, [reading] [decimal](18, 2) NOT NULL, [shift_id] [int] NULL, The value is being stored in reading column. What i want is the difference of reading between each row. i.e. if row1 has reading = 1520 and row2 has reading = 1560 then difference should be 40 and so on for row2 and row3 ..... The sample data is as below .... 67118 5 2013-02-23 04:21

ORDER BY with diacritic in Postgres

匆匆过客 提交于 2019-12-11 10:02:49
问题 I need to select data from table and sort them with ORDER BY clause. The problem is the column contains text data with czech diacritic. I cannot use COLLATE, because the DB is part of postgres cluster which was created with lc_collate = en_US.UTF-8 and I cannot afford downtime caused by recreating the cluster with correct lc_collate. Sample data: CREATE TABLE test ( id serial PRIMARY key, name text ); INSERT INTO test (name) VALUES ('Žoo'), ('Zoo'), ('ŽOO'), ('ZOO'), ('ŽoA'), ('ŽóA'), ('ŽoÁ')