query-optimization

mysql, ifnull vs coalesce, which is faster?

旧时模样 提交于 2019-11-26 11:27:12
问题 if it\'s known that there are only two values to candidate for the result of a column, ifnull(a, b) as a_or_b_1 and coalesce(a, b) as a_or_b_2 will give the same result. but which is faster? when searching i found this article, which says ifnull is faster. but it was the only article i found. any views on this? thanks in advance :) 回答1: My view is that you should benchmark for your usage. I doubt there will be much difference. Bear in mind that while a single benchmark might suggest that one

Why does direction of index matter in MongoDB?

独自空忆成欢 提交于 2019-11-26 10:09:48
问题 To quote the docs: When creating an index, the number associated with a key specifies the direction of the index, so it should always be 1 (ascending) or -1 (descending). Direction doesn\'t matter for single key indexes or for random access retrieval but is important if you are doing sorts or range queries on compound indexes. However, I see no reason why direction of the index should matter on compound indexes. Can someone please provide a further explanation (or an example)? 回答1: MongoDB

Sql: How to properly check if a record exists

让人想犯罪 __ 提交于 2019-11-26 09:15:53
问题 Reading some SQL Tuning documentation I found this: Select count(*) : - Counts the number of rows - Often is improperly used to verify the existence of a record Is Select count(*) really that bad? What\'s the proper way to verify the existence of a record? 回答1: It's better to use either of the following: -- Method 1. SELECT 1 FROM table_name WHERE unique_key = value; -- Method 2. SELECT COUNT(1) FROM table_name WHERE unique_key = value; The first alternative should give you no result or one

Checking multiple columns for one value

混江龙づ霸主 提交于 2019-11-26 09:08:01
问题 I have a table that has columns like this for example: id,col1,col2,col3,col4 Now, I want to check if ANY of col1, col2, col3, col4 have the passed in value. The long way to do it would be.. SELECT * FROM table WHERE (col1 = 123 OR col2 = 123 OR col3 = 123 OR col4 = 123); I guess it\'s the opposite version of IN . Is there an easier way to do what I want? 回答1: You can use the IN predicate, like so: SELECT * FROM table WHERE 123 IN(col1, col2, col3, col4); SQL Fiddle Demo it's the opposite

OR Operator Short-circuit in SQL Server

南笙酒味 提交于 2019-11-26 07:47:12
问题 I want to consult SQL Server OR short-circuit Code: DECLARE @tempTable table ( id int ) INSERT @tempTable(id) values(1) DECLARE @id varchar(10) SET @id = \'x\' SELECT * FROM @tempTable WHERE 1=1 OR id = @id --successfully SELECT * FROM @tempTable WHERE @id = \'x\' OR id = @id --Exception not Convert \'x\' to int Why? 1=1 and @id=\'x\' is true. SQL Server OR operator : whether the short-circuit function? THANKS 回答1: Within SQL, there is no requirement that an OR clause breaks early. In other

Difference in MySQL JOIN vs LEFT JOIN

和自甴很熟 提交于 2019-11-26 07:29:46
问题 I have this cross-database query... SELECT `DM_Server`.`Jobs`.*, `DM_Server`.servers.Description AS server, digital_inventory.params, products.products_id, products.products_pdfupload, customers.customers_firstname, customers.customers_lastname FROM `DM_Server`.`Jobs` INNER JOIN `DM_Server`.servers ON servers.ServerID = Jobs.Jobs_ServerID JOIN `cpod_live`.`digital_inventory` ON digital_inventory.jobname = Jobs.Jobs_Name JOIN `cpod_live`.`products` ON products.products_pdfupload = CONCAT

60 million entries, select entries from a certain month. How to optimize database?

白昼怎懂夜的黑 提交于 2019-11-26 06:48:34
问题 I have a database with 60 million entries. Every entry contains: ID DataSourceID Some Data DateTime I need to select entries from certain month. Each month contains approximately 2 million entries. select * from Entries where time between \"2010-04-01 00:00:00\" and \"2010-05-01 00:00:00\" (query takes approximately 1.5 minutes) I\'d also like to select data from certain month from a given DataSourceID. (takes approximately 20 seconds) There are about 50-100 different DataSourceIDs. Is there

mysql select from n last rows

喜欢而已 提交于 2019-11-26 06:00:28
问题 I have a table with index (autoincrement) and integer value. The table is millions of rows long. How can I search if a certain number appear in the last n rows of the table most efficiently? 回答1: Starting from the answer given by @chaos, but with a few modifications: You should always use ORDER BY if you use LIMIT . There is no implicit order guaranteed for an RDBMS table. You may usually get rows in the order of the primary key, but you can't rely on this, nor is it portable. If you order by

Subqueries with EXISTS vs IN - MySQL

风流意气都作罢 提交于 2019-11-26 04:47:42
问题 Below two queries are subqueries. Both are the same and both works fine for me. But the problem is Method 1 query takes about 10 secs to execute while Method 2 query takes under 1 sec. I was able to convert method 1 query to method 2 but I don\'t understand what\'s happening in the query. I have been trying to figure it out myself. I would really like to learn what\'s the difference between below two queries and how does the performance gain happen ? what\'s the logic behind it ? I\'m new to

MySQL indexes - what are the best practices?

守給你的承諾、 提交于 2019-11-26 03:46:41
问题 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