query-optimization

How to use many LIKE operators and use index

醉酒当歌 提交于 2019-12-19 03:32:24
问题 In my query I want find rows that match one of many LIKE operators. I know 3 ways of doing that but only one of them can use index. Lets start with table: CREATE TABLE dir ( id BIGSERIAL PRIMARY KEY, path TEXT NOT NULL ); CREATE INDEX path_idx ON dir(path TEXT_pattern_ops); After inserting sample data I can do: EXPLAIN ANALYZE SELECT id, path FROM dir WHERE path LIKE 'A%' OR path LIKE 'B%' OR path LIKE 'C%'; Above query use index correctly. Second way: EXPLAIN ANALYZE SELECT id, path FROM dir

How to use many LIKE operators and use index

耗尽温柔 提交于 2019-12-19 03:32:19
问题 In my query I want find rows that match one of many LIKE operators. I know 3 ways of doing that but only one of them can use index. Lets start with table: CREATE TABLE dir ( id BIGSERIAL PRIMARY KEY, path TEXT NOT NULL ); CREATE INDEX path_idx ON dir(path TEXT_pattern_ops); After inserting sample data I can do: EXPLAIN ANALYZE SELECT id, path FROM dir WHERE path LIKE 'A%' OR path LIKE 'B%' OR path LIKE 'C%'; Above query use index correctly. Second way: EXPLAIN ANALYZE SELECT id, path FROM dir

MySQL: comparison of integer value and string field with index

孤者浪人 提交于 2019-12-19 02:37:10
问题 Table a_table has index on string_column . I have a query: SELECT * FROM a_table WHERE string_column = 10; I used EXPLAIN to find that no indexes are used. Why? Could you help me with MySQL documentation link? Updated: Sandbox (SQL Fiddle) 回答1: The essential point is that the index cannot be used if the database has to do a conversion on the table-side of the comparison. Besides that, the DB always coverts Strings -> Numbers because this is the deterministic way (otherwise 1 could be

Why is doing a top(1) on an indexed column in SQL Server slow?

五迷三道 提交于 2019-12-18 18:46:36
问题 I'm puzzled by the following. I have a DB with around 10 million rows, and (among other indices) on 1 column (campaignid_int) is an index. Now I have 700k rows where the campaignid is indeed 3835 For all these rows, the connectionid is the same. I just want to find out this connectionid. use messaging_db; SELECT TOP (1) connectionid FROM outgoing_messages WITH (NOLOCK) WHERE (campaignid_int = 3835) Now this query takes approx 30 seconds to perform! I (with my small db knowledge) would expect

are there any Query Optimization Tools for SQL Server? [closed]

纵然是瞬间 提交于 2019-12-18 11:53:41
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . Are there any tools that do Query Optimizations for SQL Server 2005 and above? I've searched & didn't find anything. What do you recommend? If this Question was repeated before you can close it but I didn't see anything similar 回答1: The best tool I've ever used for optimizing queries in MS SQL Server, by far, is

Generic SQL that both Access and ODBC/Oracle can understand

给你一囗甜甜゛ 提交于 2019-12-18 07:11:06
问题 I have a MS Access query that is based on a linked ODBC table (Oracle). I'm troubleshooting the poor performance of the query here: Access not properly translating TOP predicate to ODBC/Oracle SQL. SELECT ri.* FROM user1_road_insp AS ri WHERE ri.insp_id = ( select top 1 ri2.insp_id from user1_road_insp ri2 where ri2.road_id = ri.road_id and year(insp_date) between [Enter a START year:] and [Enter a END year:] order by ri2.insp_date desc, ri2.length desc, ri2.insp_id ); The documentation says:

How to force nolock hint for sql server logins

喜夏-厌秋 提交于 2019-12-18 06:09:33
问题 Does anyone know of a way to force a nolock hint on all transactions issued by a certain user? I'd like to provide a login for a support team to query the production system, but I want to protect it by forcing a nolock on everything they do. I'm using SQL Server 2005. 回答1: This is a painful and hacky way to do it, but it's what we're doing where I work. We're also using classic asp so we're using inline sql calls. we actually wrap the sql call in a function (here you can check for a specific

How to force nolock hint for sql server logins

╄→尐↘猪︶ㄣ 提交于 2019-12-18 06:09:31
问题 Does anyone know of a way to force a nolock hint on all transactions issued by a certain user? I'd like to provide a login for a support team to query the production system, but I want to protect it by forcing a nolock on everything they do. I'm using SQL Server 2005. 回答1: This is a painful and hacky way to do it, but it's what we're doing where I work. We're also using classic asp so we're using inline sql calls. we actually wrap the sql call in a function (here you can check for a specific

How can I force a subquery to perform as well as a #temp table?

橙三吉。 提交于 2019-12-18 04:30:15
问题 I am re-iterating the question asked by Mongus Pong Why would using a temp table be faster than a nested query? which doesn't have an answer that works for me. Most of us at some point find that when a nested query reaches a certain complexity it needs to broken into temp tables to keep it performative. It is absurd that this could ever be the most practical way forward and means these processes can no longer be made into a view. And often 3rd party BI apps will only play nicely with views so

Fastest way to find string by substring in SQL?

筅森魡賤 提交于 2019-12-17 23:38:34
问题 I have huge table with 2 columns: Id and Title. Id is bigint and I'm free to choose type of Title column: varchar, char, text, whatever. Column Title contains random text strings like "abcdefg", "q", "allyourbasebelongtous" with maximum of 255 chars. My task is to get strings by given substring. Substrings also have random length and can be start, middle or end of strings. The most obvious way to perform it: SELECT * FROM t LIKE '%abc%' I don't care about INSERT, I need only to do fast