query-optimization

How to interpret the output of MySQL EXPLAIN?

假如想象 提交于 2019-12-12 08:27:12
问题 I want to select the content of the column text from entrytable . EXPLAIN SELECT text FROM entrytable WHERE user = 'username' && `status` = '1' && ( `status_spam_user` = 'no_spam' || ( `status_spam_user` = 'neutral' && `status_spam_system` = 'neutral' ) ) ORDER BY datum DESC LIMIT 6430 , 10 The table has three indices: index_user (user) index_datum (datum) index_status_mit_spam (status, status_spam_user, status_spam_system) The EXPLAIN result is: id select_type table type possible_keys key

Sum for multiple date ranges in a single call?

北战南征 提交于 2019-12-12 07:52:02
问题 I have the following query: SELECT SUM("balance_transactions"."fee") AS sum_id FROM "balance_transactions" JOIN charges ON balance_transactions.source = charges.balance_id WHERE "balance_transactions"."account_id" = 6 AND (balance_transactions.type = 'charge' AND charges.refunded = false AND charges.invoice IS NOT NULL) AND ("balance_transactions"."created" BETWEEN '2013-12-20' AND '2014-01-19'); What that does is adds up all the "fees" that occurred between those two dates. Great. Works fine

Large Number of columns, Select all takes forever

随声附和 提交于 2019-12-12 06:59:55
问题 There are 210 columns in my table with around 10000 rows. Each row is unique and there is a primary key on the table. The thing is we always had to do select all query on the table to get data of all the sites. Currently, the problem is it takes too much time and the data returned is around 10mb and it will be large in the future. The table has varchar, text and date types in it. Is there any way I can modify the structure or something to make my retrieval faster. More indexing or breaking

Optimize mysql query

我们两清 提交于 2019-12-12 05:07:07
问题 I want to optimize this query as it is taking long to execute almost a second Here's the query: IF Exists( Select CustFirstName From Customers Where (CustFirstName = InputCustFirstName) OR (CustLastName= InputCustLastName) OR (Email = InputEmail) ); All these three columns have Unique index on it. and I have 765704 records in it. This is the explain result set of my query : ----+-------------+-------+------+----------------------+------+---------+------+--------+------------------------------

Getting duplicate rows by several columns in MySQL

霸气de小男生 提交于 2019-12-12 04:57:07
问题 I'm trying to search duplicate rows by several columns in large table (near 18 000 rows). Problem is that queries take a lot of time, I tried this: SELECT * FROM table_name a, table_name b WHERE a.col1 = b.col1 AND a.col2 = b.col2 AND a.col3 = b.col3 AND a.col4 = b.col4 AND a.id <> b.id and this: SELECT * FROM table_name WHERE col1 IN ( SELECT col1 FROM table_name GROUP BY col1 HAVING count(col1) > 1 ) AND col2 IN ( SELECT col2 FROM table_name GROUP BY col2 HAVING count(col2) > 1 ) AND col3

MySQL: Optimized query to find matching strings from set of strings

你离开我真会死。 提交于 2019-12-12 04:52:52
问题 I am having 10 sets of strings each set having 9 strings. Of this 10 sets, all strings in first set have length 10, those in second set have length 9 and so on. Finally, all strings in 10th set have length 1. There is common prefix of (length-2) characters in each set. And the prefix length reduces by 1 in next set. Thus, first set has 8 characters in common, second has 7 and so on. Here is what a sample of 10 sets look like: pu3q0k0vwn pu3q0k0vwp pu3q0k0vwr pu3q0k0vwq pu3q0k0vwm pu3q0k0vwj

Building Dynamic Query Using Case in Where Clause

自古美人都是妖i 提交于 2019-12-12 04:39:30
问题 I have a stored procedure and I want implement the following query using Case Statement , but I am not sure how to do it . pseudocode of what I want is provided here : declare @PI_X decimal(18); declare @PI_y decimal (18); SELECT F1, F2,F3 FROM TABLE T WHERE CASE WHEN @PI_X IS NULL THEN @PI_Y = T.Y WHEN @PI_Y IS NULL THEN @PI_X = T.X It seems that using case statement for conditions is not true and its for values . NOTE: I want to run this query in DB2 and SQL server , but really Data Base

SQL optimization with indexes

杀马特。学长 韩版系。学妹 提交于 2019-12-12 03:36:18
问题 I have been trying hard to optimize below query using indexes in place of where clause. can any one help on the same. Data volume is very high so I want to optimize it using indexing or anyother way? Query goes like this: SELECT * FROM (SELECT a.*, rownum as row_num FROM (SELECT DISTINCT lot.lot_no AS lotNo, lot.sel as sel, lot.C_ARRIVAL_NOTICE_NO AS arrNoticeNo, lot.C_SHIPMENT_DIRECTION AS shipmentDirection, lot.C_SENDERS_REFERENCE_NUM AS externalReference, lot.booking_seq AS bookingNo,lot

Check existence of distinct values for each group

最后都变了- 提交于 2019-12-12 03:22:21
问题 EDITED: Suppose I have the following table in MySQL: CREATE TABLE `events` ( `pv_name` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL, `time_stamp` bigint(20) UNSIGNED NOT NULL, `value` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin, PRIMARY KEY (`pv_name`, `time_stamp`) ) ENGINE=InnoDB; I can find each pv_name that has more than one distinct value in this table using the following query: SELECT events.pv_name FROM events GROUP BY events.pv_name HAVING COUNT(DISTINCT events.value) > 1; The

How to use unique column values as input into another select statement

最后都变了- 提交于 2019-12-12 02:55:58
问题 I have a table (MySQL) that has a column called binID. The values in this column can range from 1 to 70. What I want to do is select the unique values of this column (should be the numbers from 1 to 70), then iterate over them using each (lets call it theBinID) as a parameter into another SELECT statement such as: SELECT * FROM MyTable WHERE binID = theBinID ORDER BY createdDate DESC LIMIT 10 Basically, I am looking to get the 10 most recent rows for each binID. I do not believe there is a