sql-limit

How to optimize MySQL “Order By Limit 1” in queries that join multiple tables?

心不动则不痛 提交于 2019-12-10 11:32:34
问题 So I have a query like this: SELECT tablea.name, tablea.views from tablea inner join tableb on (tablea.id = tableb.id and tablea.balance > 0) order by tablea.views asc limit 1 However, the problem is that when I run it, it runs quite slow (4+ seconds). Interestingly, when the 'order by' clause is removed, while keeping the limit 1, it runs in 0.005 seconds (approx). Even more interestingly: when I don't join it to tableb, i.e.: SELECT tablea.name, tablea.views from tablea where tablea.balance

UPDATE .. LIMIT 1 with SqlAlchemy and PostgreSQL

↘锁芯ラ 提交于 2019-12-09 03:16:55
问题 With SqlAlchemy, is it possible to build a query which will update only the first matching row? In my case, I need to update the most recent log entry: class Log(Base): __tablename__ = 'logs' id = Column(Integer, primary_key=True) #... analyzed = Column(Boolean) session.query(Log) \ .order_by(Log.id.desc()) \ .limit(1) \ .update({ 'analyzed': True }) Which results into: InvalidRequestError: Can't call Query.update() when limit() has been called It makes sense, since UPDATE ... LIMIT 1 is a

MYSQL: Limiting rows per whereIn()

我与影子孤独终老i 提交于 2019-12-08 12:48:36
问题 users Table id user_comments Table id | user_id | content | created_at I have a list of user IDs, and I want to grab the latest 3 comments for each user id. SELECT * FROM user_comments WHERE user_id IN (1, 2, 3, 4, 5) ORDER BY created_at DESC LIMIT 3; This will grab the last 3 comments from all matching IDs, I want the last 3 comments for each ID. 1 query without unions preferred. I have tried right joining the table on itself but I cant seem to get it right. ** Edit: I cannot rely on the id

Limit join to one row

自古美人都是妖i 提交于 2019-12-07 06:24:28
问题 I have the following query: SELECT sum((select count(*) as itemCount) * "SalesOrderItems"."price") as amount, 'rma' as "creditType", "Clients"."company" as "client", "Clients".id as "ClientId", "Rmas".* FROM "Rmas" JOIN "EsnsRmas" on("EsnsRmas"."RmaId" = "Rmas"."id") JOIN "Esns" on ("Esns".id = "EsnsRmas"."EsnId") JOIN "EsnsSalesOrderItems" on("EsnsSalesOrderItems"."EsnId" = "Esns"."id" ) JOIN "SalesOrderItems" on("SalesOrderItems"."id" = "EsnsSalesOrderItems"."SalesOrderItemId") JOIN

How to optimize MySQL “Order By Limit 1” in queries that join multiple tables?

坚强是说给别人听的谎言 提交于 2019-12-06 10:24:01
So I have a query like this: SELECT tablea.name, tablea.views from tablea inner join tableb on (tablea.id = tableb.id and tablea.balance > 0) order by tablea.views asc limit 1 However, the problem is that when I run it, it runs quite slow (4+ seconds). Interestingly, when the 'order by' clause is removed, while keeping the limit 1, it runs in 0.005 seconds (approx). Even more interestingly: when I don't join it to tableb, i.e.: SELECT tablea.name, tablea.views from tablea where tablea.balance > 0 order by tablea.views asc limit 1 The query runs in 0.005 seconds usually. Notes: The column views

Update top N values using PostgreSQL

别来无恙 提交于 2019-12-04 00:33:44
I want to update the top 10 values of a column in table. I have three columns; id , account and accountrank . To get the top 10 values I can use the following: SELECT * FROM accountrecords ORDER BY account DESC LIMIT 10; What I would like to do is to set the value in accountrank to be a series of 1 - 10 , based on the magnitude of account . Is this possible to do in PostgreSQL? WITH cte AS ( SELECT id, row_number() OVER (ORDER BY account DESC NULLS LAST) AS rn FROM accountrecords ORDER BY account DESC NULLS LAST LIMIT 10 ) UPDATE accountrecords a SET accountrank = cte.rn FROM cte WHERE cte.id

Select first record if none match

非 Y 不嫁゛ 提交于 2019-12-02 00:56:42
问题 In PostgreSQL, I would like to select a row based on some criteria, but if no row matches the criteria, I would like to return the first row. The table actually contains an ordinal column, so the task should be easier (the first row is the one with ordinal 0). For example: SELECT street, zip, city FROM address WHERE street LIKE 'Test%' OR ord = 0 LIMIT 1; But in this case, there is no way to guarantee the order of the records that match, and I have nothing to order them by. What would be the

How to limit the results on a SQL query

天涯浪子 提交于 2019-12-01 18:11:23
问题 I'm wondering is it possible to limit the result of a SQL request? For example, only return up to 50 rows from: SELECT * FROM <table> thanks. 回答1: Yes, this is possible. This differs between db engines. Postgres: SELECT * FROM <table> LIMIT 50 SQL Server: SELECT TOP 50 * FROM <table> ... 回答2: You could use the TOP clause: SELECT TOP 50 * FROM <table> If your database doesn't support it you may try also LIMIT and ROWNUM but once again this will depend on the database you are using. 回答3: Yes of

MySQL syntax error using LIMIT command with Prepared Statement in Java

ⅰ亾dé卋堺 提交于 2019-12-01 08:41:23
I am writing code in Java and I want to take every time I run this code the next line from a MySQL table.The second time I run this code is this. String timh1 = "1"; String timh2 = "2"; PreparedStatement st = null; String sqlGrammes = "SELECT SURNAME ,KATHGORIA, AFM , NAME FROM EMPLOYEE LIMIT ?,? "; try { st = connection.prepareStatement(sqlGrammes); st.setString(1, timh1); st.setString(2, timh2); But it shows me this error : com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the

UPDATE .. LIMIT 1 with SqlAlchemy and PostgreSQL

ぃ、小莉子 提交于 2019-12-01 06:22:54
With SqlAlchemy, is it possible to build a query which will update only the first matching row? In my case, I need to update the most recent log entry: class Log(Base): __tablename__ = 'logs' id = Column(Integer, primary_key=True) #... analyzed = Column(Boolean) session.query(Log) \ .order_by(Log.id.desc()) \ .limit(1) \ .update({ 'analyzed': True }) Which results into: InvalidRequestError: Can't call Query.update() when limit() has been called It makes sense, since UPDATE ... LIMIT 1 is a MySQL-only feature (with the solution given here ) But how would I do the same with PostgreSQL? Possibly,