query-optimization

How to estimate SQL query timing?

狂风中的少年 提交于 2019-11-26 16:00:11
问题 I'm trying to get an rough (order-of-magnitude) estimate of how long time the following query could take: mysql> EXPLAIN SELECT t1.col1, t1_col4 FROM t1 LEFT JOIN t2 ON t1.col1=t2.col1 WHERE col2=0 AND col3 IS NULL; +----+-------------+--------------------+------+---------------+------------+---------+-----------------------------+---------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------------

Avoid Naming User Stored Procedures SP% or SP_%

我是研究僧i 提交于 2019-11-26 14:52:13
问题 Does a user stored procedure with the prefix of SP (not SP_) have poorer performance by looking in the Master DB (similar to user SP named SP_) or does it look in the DB where the stored procedure exists because it lack an underscore? 回答1: The reserved prefix that exhibits this behaviour is sp_ . Not sp . A stored procedure called spAddUser will be resolved in the normal way without looking for a matching object in master . The relevant quote in books online is Avoid the use of the sp_ prefix

Does assigning stored procedure input parameters to local variables help optimize the query?

一个人想着一个人 提交于 2019-11-26 14:40:10
问题 I have a stored procedure that takes 5 input parameters. The procedure is a bit complicated and takes around 2 minutes to execute. I am in process of optimizing query. So, my question is, does it always help to assign input parameters to local variables and then use local variables in the procedure? If so, how does it help? 回答1: I will not try and explain the full details of parameter sniffing, but in short, no it does not always help (and it can hinder). Imagine a table (T) with a primary

MySQL: Can I do a left join and pull only one row from the join table?

て烟熏妆下的殇ゞ 提交于 2019-11-26 14:09:48
问题 I wrote a custom help desk for work and it's been running great... until recently. One query has really slowed down. It takes about 14 seconds now! Here are the relevant tables: CREATE TABLE `tickets` ( `id` int(11) unsigned NOT NULL DEFAULT '0', `date_submitted` datetime DEFAULT NULL, `date_closed` datetime DEFAULT NULL, `first_name` varchar(50) DEFAULT NULL, `last_name` varchar(50) DEFAULT NULL, `email` varchar(50) DEFAULT NULL, `description` text, `agent_id` smallint(5) unsigned NOT NULL

MySQL indexes - what are the best practices?

折月煮酒 提交于 2019-11-26 13:53:18
I've been using indexes on my MySQL databases for a while now but never properly learnt about them. Generally I put an index on any fields that I will be searching or selecting using a WHERE clause but sometimes it doesn't seem so black and white. What are the best practices for MySQL indexes? Example situations/dilemmas: If a table has six columns and all of them are searchable, should I index all of them or none of them? . What are the negative performance impacts of indexing? . If I have a VARCHAR 2500 column which is searchable from parts of my site, should I index it? timdev You should

Mysql count performance on very big tables

前提是你 提交于 2019-11-26 13:05:50
问题 I have a table with more than 100 millions rows in Innodb. I have to know if there is more than 5000 rows where the foreign key = 1. I don\'t need the exact number. I made some testing : SELECT COUNT(*) FROM table WHERE fk = 1 => 16 seconds SELECT COUNT(*) FROM table WHERE fk = 1 LIMIT 5000 => 16 seconds SELECT primary FROM table WHERE fk = 1 => 0.6 seconds I will have a bigger network and treatment time but it can be an overload of 15.4 seconds ! Do you have a better idea ? Thanks Edit:

Optimize Postgres timestamp query range

穿精又带淫゛_ 提交于 2019-11-26 12:29:49
问题 I have the following table and indices defined: CREATE TABLE ticket ( wid bigint NOT NULL DEFAULT nextval(\'tickets_id_seq\'::regclass), eid bigint, created timestamp with time zone NOT NULL DEFAULT now(), status integer NOT NULL DEFAULT 0, argsxml text, moduleid character varying(255), source_id bigint, file_type_id bigint, file_name character varying(255), status_reason character varying(255), ... ) I created an index on the created timestamp as follows: CREATE INDEX ticket_1_idx ON ticket

How to find out what is locking my tables?

假如想象 提交于 2019-11-26 12:02:04
问题 I have a SQL table that all of a sudden cannot return data unless I include with (nolock) on the end, which indicates some kind of lock left on my table. I\'ve experimented a bit with sys.dm_tran_locks to identify that there are in fact a number of locks on the table, but how do I identify what is locking them (ie the request element of the sys.dm_tran_locks)? EDIT: I know about sp_lock for pre SQL 2005, but now that that sp is deprecated, AFAIK the right way to do this is with sys.dm_tran

PostgreSQL - fetch the row which has the Max value for a column

我们两清 提交于 2019-11-26 11:48:40
问题 I\'m dealing with a Postgres table (called \"lives\") that contains records with columns for time_stamp, usr_id, transaction_id, and lives_remaining. I need a query that will give me the most recent lives_remaining total for each usr_id There are multiple users (distinct usr_id\'s) time_stamp is not a unique identifier: sometimes user events (one by row in the table) will occur with the same time_stamp. trans_id is unique only for very small time ranges: over time it repeats remaining_lives

Mysql Improve Search Performance with wildcards (%%)

淺唱寂寞╮ 提交于 2019-11-26 11:28:04
问题 Below is a query I use for searching a person by email SELECT * FROM phppos_customers JOIN phppos_people ON phppos_customers.person_id = phppos_people.person_id WHERE deleted = 0 AND email LIKE \'%f%\' ORDER BY email ASC Will adding an index on \"email\" speed up the query? 回答1: No, because MySQL will not be able to utilize the index when you have a leading wildcard. If you changed your LIKE to 'f%', then it would be able to use the index. 回答2: No, Mysql will not use the index because LIKE