sql-limit

Any point in using LIMIT in EXISTS query?

有些话、适合烂在心里 提交于 2020-03-18 03:49:57
问题 Is there any performance benefit in adding a LIMIT to an EXISTS query, or would MySQL apply the limit on its own? Example: IF EXISTS ( SELECT 1 FROM my_table LIMIT 1 -- can this improve performance? ) THEN ... END IF; 回答1: The purpose of EXISTS() is to perform the query only until it can decide if there are any rows in that table matching the WHERE clause. That is, it logically does the same thing as LIMIT 1 . EXISTS is probably called semi-join in some circles. Bottom line: Don't use LIMIT 1

Laravel - randomly select n number of rows containing same value in certain column after applying 'order by'

▼魔方 西西 提交于 2020-02-06 09:27:13
问题 In my Laravel project, in the database table ads , I have the following structure : id | col1 | col2 col2 has values like topad , bump , urgent along with empty value. I want to take all the rows from the ads table and sort them alphabetically based on col2 in descending order. So I used: Ads::orderBy('col2','DESC')->get() Now I have 2 conditions to be applied on the query. 1st condition : Suppose there are 4 rows with topad in col2 , 5 rows with urgent in col2 , 6 rows with bump in col2 and

Laravel - randomly select n number of rows containing same value in certain column after applying 'order by'

烂漫一生 提交于 2020-02-06 09:23:30
问题 In my Laravel project, in the database table ads , I have the following structure : id | col1 | col2 col2 has values like topad , bump , urgent along with empty value. I want to take all the rows from the ads table and sort them alphabetically based on col2 in descending order. So I used: Ads::orderBy('col2','DESC')->get() Now I have 2 conditions to be applied on the query. 1st condition : Suppose there are 4 rows with topad in col2 , 5 rows with urgent in col2 , 6 rows with bump in col2 and

DB2 Using LIMIT and OFFSET

别来无恙 提交于 2020-01-09 11:10:11
问题 I am developing a Java Web service allow paging when fetching big data set from a DB2 Database on a IBM Mid Range Machine (AS400). For example; if there are 10000 records in a data set, I want to fetch them in 1000 blocks at a time. I found this article that explains that I can use LIMIT, and OFFSET. But I need to set the DB2_COMPATIBILITY_VECTOR variable to MYS . Now I have been googling and saw you can use the db2set to set this variable. But I have not been able to find out where to type

Retrieving only a fixed number of rows in MySQL

不想你离开。 提交于 2020-01-09 10:00:54
问题 I am testing my database design under load and I need to retrieve only a fixed number of rows (5000) I can specify a LIMIT to achieve this, however it seems that the query builds the result set of all rows that match and then returns only the number of rows specified in the limit. Is that how it is implemented? Is there a for MySQL to read one row, read another one and basically stop when it retrieves the 5000th matching row? 回答1: MySQL is smart in that if you specify a LIMIT 5000 in your

Limit number of rows per group from join (NOT to 1 row)

荒凉一梦 提交于 2020-01-04 04:10:05
问题 Given these tables: TABLE Stores ( store_id INT, store_name VARCHAR, etc ); TABLE Employees ( employee_id INT, store_id INT, employee_name VARCHAR, currently_employed BOOLEAN, etc ); I want to list the 15 longest-employed employees for each store (let's say the 15 with lowest employee_id ), or ALL employees for a store if there are 15 who are currently_employed='t' . I want to do it with a join clause. I've found a lot of examples of people doing this only for 1 row, usually a min or max

Mysql - return last 3 results in table

我的梦境 提交于 2019-12-24 06:17:33
问题 i was wondering if there was an easy way with just an sql statement to return the last three results in the table but in that order i.e. if there are a hundered results it would return in the order of 98, 99, 100 not simply ordering by id DESC and limit 3 which would return in order 100, 99, 98 Any help much appreciated. p.s. in this instance, lets say I don't know the amount of results and don't really want to send 2 sql requests just to find the amount ( for any OFFSET answers ). 回答1: One

CakePHP: Limit Fields associated with a model

百般思念 提交于 2019-12-23 17:39:04
问题 I have several fields in some of my database tables that my CakePHP models never need to retrieve. Is there some way to set a default set of fields to fetch at the model level? For instance I retrieve some data from a third party designed database that has 50 fields per table, I use 5. I know I can set limits on fields at the time of the find() query and at the time of any associations between models, but I was wondering if there was a model-level approach. 回答1: CakePHP does not offer what

Update top N values using PostgreSQL

你说的曾经没有我的故事 提交于 2019-12-21 07:19:15
问题 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? 回答1: WITH cte AS ( SELECT id, row_number() OVER (ORDER BY account DESC NULLS LAST) AS rn FROM accountrecords ORDER

No MySQL records return when past a certain limit? [closed]

我怕爱的太早我们不能终老 提交于 2019-12-13 10:52:11
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago . I have the following query which returns 250 records: SELECT DISTINCT p.* FROM Persons AS p INNER JOIN Colors AS c ON c.ColorId = p.FavoriteColorId WHERE p.Name = 'John Doe' AND c.ColorName IN ('RED','BLUE','YELLOW') LIMIT 240,10; -- Returns 198 records SELECT DISTINCT p.* FROM Persons AS p INNER