query-optimization

Why does MySQL not always use index for select query?

旧巷老猫 提交于 2020-06-01 06:21:31
问题 I have two tables in my database users and articles. Records in my users and articles table are given below: +----+--------+ | id | name | +----+--------+ | 1 | user1 | | 2 | user2 | | 3 | user3 | +----+--------+ +----+---------+----------+ | id | user_id | article | +----+---------+----------+ | 1 | 1 | article1 | | 2 | 1 | article2 | | 3 | 1 | article3 | | 4 | 2 | article4 | | 5 | 2 | article5 | | 6 | 3 | article6 | +----+---------+----------+ Given below the queries and the respected

Using an Index with Mongo's $first Group Operator

十年热恋 提交于 2020-05-16 22:00:20
问题 Per Mongo's latest $group documentation, there is a special optimization for $first: Optimization to Return the First Document of Each Group If a pipeline sorts and groups by the same field and the $group stage only uses the $first accumulator operator, consider adding an index on the grouped field which matches the sort order. In some cases, the $group stage can use the index to quickly find the first document of each group. It makes sense, since only the first entry in an ordered index

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

What index should I use when using JOIN on PRIMARY KEY

ぐ巨炮叔叔 提交于 2020-02-25 04:16:59
问题 I'm trying to optimise the following MySQL query SELECT Hotel.HotelId, Hotel.Name, Hotel.Enabled, Hotel.IsClosed, HotelRoom.HotelId, HotelRoom.RoomId, HotelRoom.Name AS RoomName FROM Hotel INNER JOIN HotelRoom ON Hotel.HotelId = HotelRoom.HotelId WHERE Hotel.IsClosed = 0 AND Hotel.Enabled = 1 AND HotelRoom.Deleted = 0 AND HotelRoom.Enabled = 1 AND IF(LENGTH(TRIM(sAuxiliaryIds)) > 0 AND sAuxiliaryIds IS NOT NULL, FIND_IN_SET(Hotel.AuxiliaryId, sAuxiliaryIds), 1=1) > 0 ORDER BY Hotel.HotelId

Abort subsequent UNION ALL commands if match found [H2]

老子叫甜甜 提交于 2020-02-05 08:44:08
问题 How do I adapt the query below so that it doesn't perform unnecessary UNION ALL unless the SELECT statement above it doesn't find a match? SELECT LATITUDE, LONGITUDE FROM coordinates WHERE address = ? AND community = ? UNION ALL SELECT LATITUDE, LONGITUDE FROM coordinates WHERE address::text = ? AND community::text LIKE ? UNION ALL SELECT LATITUDE, LONGITUDE FROM coordinates WHERE address::text LIKE ? AND community::text LIKE ? My coordinates table has columns | ID | ADDRESS | CITY | LATITUDE

Abort subsequent UNION ALL commands if match found [H2]

橙三吉。 提交于 2020-02-05 08:44:07
问题 How do I adapt the query below so that it doesn't perform unnecessary UNION ALL unless the SELECT statement above it doesn't find a match? SELECT LATITUDE, LONGITUDE FROM coordinates WHERE address = ? AND community = ? UNION ALL SELECT LATITUDE, LONGITUDE FROM coordinates WHERE address::text = ? AND community::text LIKE ? UNION ALL SELECT LATITUDE, LONGITUDE FROM coordinates WHERE address::text LIKE ? AND community::text LIKE ? My coordinates table has columns | ID | ADDRESS | CITY | LATITUDE

Force MySQL to use two indexes on a Join

流过昼夜 提交于 2020-01-31 03:20:10
问题 I am trying to force MySQL to use two indexes. I am joining a table and I want to utilize the cross between the two indexes. The specific term is Using intersect and here is a link to MySQL documentation: http://dev.mysql.com/doc/refman/5.0/en/index-merge-optimization.html Is there any way to force this implementation? My query was using it (and it sped stuff up), but now for whatever reason it has stopped. Here is the JOIN I want to do this on. The two indexes I want the query to use are scs

Selecting primary key:Why postgres prefers to do sequential scan vs index scan

懵懂的女人 提交于 2020-01-25 23:48:00
问题 I have the following table create table log ( id bigint default nextval('log_id_seq'::regclass) not null constraint log_pkey primary key, level integer, category varchar(255), log_time timestamp, prefix text, message text ); It contains like 3 million of rows. I'm comparing the following queries: EXPLAIN SELECT id FROM log WHERE log_time < now() - INTERVAL '3 month' LIMIT 100000 which yields the following plan: Limit (cost=0.00..19498.87 rows=100000 width=8) -> Seq Scan on log (cost=0.00.

Experience with when to use OPTIMIZE FOR UNKNOWN

别说谁变了你拦得住时间么 提交于 2020-01-21 10:08:17
问题 I have read the theory and views behind SQL Server 2008's "OPTIMIZE FOR UNKNOWN" query plan option. I understand what it does well enough. I did some limited experiments and found that with a warm cache, it only was of benefit on > 100k rows. However, this was on a simple table and query with no joins, filtering, etc. On a cold cache, the picture would undoubtedly be much more in its favor. I don't currently have a production system to bench the before/after. So I am curious if anyone has

How can I optimize a query that returns a lot of records, then order by a date field and return only the latest one? [duplicate]

别说谁变了你拦得住时间么 提交于 2020-01-17 22:39:24
问题 This question already has answers here : Optimizing a query returning a lot of records, a way to avoid hundreds of join. Is it a smart solution? (3 answers) Closed 2 years ago . I am not so into database and I have the following doubt about the possible optimization of this query (defined on a MySql database): SELECT MCPS.id AS series_id, MD_CD.market_details_id AS market_id, MD_CD.commodity_details_id AS commodity_id, MD.market_name AS market_name, MCPS.price_date AS price_date, MCPS.avg