sql-order-by

Optimize query with OFFSET on large table

白昼怎懂夜的黑 提交于 2019-11-26 17:48:46
I have table create table big_table ( id serial primary key, -- other columns here vote int ); This table is very big, approximately 70 million rows, I need to query: SELECT * FROM big_table ORDER BY vote [ASC|DESC], id [ASC|DESC] OFFSET x LIMIT n -- I need this for pagination As you may know, when x is a large number, queries like this are very slow. For performance optimization I added indexes: create index vote_order_asc on big_table (vote asc, id asc); and create index vote_order_desc on big_table (vote desc, id desc); EXPLAIN shows that the above SELECT query uses these indexes, but it's

When no 'Order by' is specified, what order does a query choose for your record set?

本秂侑毒 提交于 2019-11-26 17:44:14
I was always of the impression that a query with no specified 'Order by' rule, would order this by the results by what was specified within your where clause. For instance, my where clause states: WHERE RESULTS_I_AM_SEARCHING_FOR IN ITEM 1 ITEM 2 ITEM 3 I would have imagined that the results returned for items 1, 2 and 3 would be in the order specified in the where, however this is not the case. Does anyone know what order it sorts them in when not specified? Thanks and sorry for the really basic question! Damon marc_s If you don't specify an ORDER BY , then there is NO ORDER defined. The

How to SORT in order as entered in SQL Server?

不问归期 提交于 2019-11-26 17:19:55
问题 I'm using SQL Server and I'm trying to find results but I would like to get the results in the same order as I had input the conditions. My code: SELECT AccountNumber, EndDate FROM Accounts WHERE AccountNumber IN (212345, 312345, 145687, 658975, 256987, 365874, 568974, 124578, 125689) -- I would like the results to be in the same order as these numbers. 回答1: Here is an in-line approach Example Declare @List varchar(max)='212345, 312345, 145687, 658975, 256987, 365874, 568974, 124578, 125689'

How to use DISTINCT and ORDER BY in same SELECT statement?

谁说我不能喝 提交于 2019-11-26 17:17:55
After executing the following statement: SELECT Category FROM MonitoringJob ORDER BY CreationDate DESC I am getting the following values from the database: test3 test3 bildung test4 test3 test2 test1 but I want the duplicates removed, like this: bildung test4 test3 test2 test1 I tried to use DISTINCT but it doesn't work with ORDER BY in one statement. Please help. Important: I tried it with: SELECT DISTINCT Category FROM MonitoringJob ORDER BY CreationDate DESC it doesn't work. Order by CreationDate is very important. The problem is that the columns used in the ORDER BY aren't specified in the

Conditional SQL ORDER BY ASC/DESC for alpha columns

≡放荡痞女 提交于 2019-11-26 16:43:51
问题 Writing a stored procedure in MS SQL Server 2008 R2, I want to avoid using DSQL... I would like the sort method (ASC or DESC) to be conditional. Now, with a numeric column I would simply use a case statement and negate the value to emulate ASC or DESC... That is: ... ORDER BY CASE @OrderAscOrDesc WHEN 0 THEN [NumericColumn] ELSE -[NumericColumn] END ASC What is an appropriate method for doing this with an alpha column? EDIT: I thought of a clever way but it seems terribly inefficient... I

Dynamic Order in JDBI SQL Object Queries

寵の児 提交于 2019-11-26 16:36:01
问题 How do you do ordering with SQL Object Queries in JDBI? I want to do something like: @SqlQuery( "SELECT * FROM users " + "WHERE something = :something " + "ORDER BY :orderBy :orderDir" ) List<User> getUsers( @Bind("something") Integer something , @BindOrderBy("orderBy") String orderBy , @BindOrderDir("orderDir") String orderDir ); or @SqlQuery( "SELECT * FROM users " + "WHERE something = :something " + "ORDER BY :orderBy :orderDir" ) List<User> getUsers( @Bind("something") Integer something ,

ORDER BY ASC with Nulls at the Bottom

丶灬走出姿态 提交于 2019-11-26 16:20:41
问题 I'm writing an SQL query that connects a schools table to a districts table. Simple One-To-Many relationship where each school is attached to one district. My query is as follows: SELECT schools.id AS schoolid, schools.name AS school, districts.id AS districtid, districts.name AS district FROM sms_schools AS schools LEFT JOIN sms_districts AS districts ON schools.districtid = districts.id WHERE 1 = 1 ORDER BY districts.name, schools.name The reason I did a left join is because not every

ORDER BY date and time BEFORE GROUP BY name in mysql

走远了吗. 提交于 2019-11-26 15:57:28
问题 i have a table like this: name date time tom | 2011-07-04 | 01:09:52 tom | 2011-07-04 | 01:09:52 mad | 2011-07-04 | 02:10:53 mad | 2009-06-03 | 00:01:01 i want oldest name first: SELECT * ORDER BY date ASC, time ASC GROUP BY name (->doesn't work!) now it should give me first mad(has earlier date) then tom but with GROUP BY name ORDER BY date ASC, time ASC gives me the newer mad first because it groups before it sorts! again: the problem is that i can't sort by date and time before i group

What does “ORDER BY (SELECT NULL)” mean?

北城以北 提交于 2019-11-26 15:52:15
问题 The following SQL is from Itzik Ben-Gan that is used to generate a numbers table. What does the order by (select null) part mean? Thanks. DECLARE @number_of_numbers INT; SELECT @number_of_numbers = 100000; WITH a AS ( SELECT 1 AS i UNION ALL SELECT 1 ), b AS ( SELECT 1 AS i FROM a AS x , a AS y ), c AS ( SELECT 1 AS i FROM b AS x , b AS y ), d AS ( SELECT 1 AS i FROM c AS x , c AS y ), e AS ( SELECT 1 AS i FROM d AS x , d AS y ), f AS ( SELECT 1 AS i FROM e AS x , e AS y ), numbers AS (

Alphanumeric sorting with PostgreSQL

不打扰是莪最后的温柔 提交于 2019-11-26 15:48:24
问题 In the database, I have various alpha-numeric strings in the following format: 10_asdaasda 100_inkskabsjd 11_kancaascjas 45_aksndsialcn 22_dsdaskjca 100_skdnascbka I want them to essentially be sorted by the number in front of the string and then the string name itself, but of course, characters are compared one by one and so the result of Order by name produces: 10_asdaasda 100_inkskabsjd 100_skdnascbka 11_kancaascjas 22_dsdaskjca 45_aksndsialcn instead of the order I'd prefer: 10_asdaasda