English 中文(简体)
MySQL:另一个领域指数化的缺点是什么?
原标题:MySQL: What are the drawbacks of indexing an additional field?

我的口号是:

CREATE TABLE  UserTrans (
 `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
   `transaction_id` varchar(255) NOT NULL default  0 ,
  `source` varchar(100) NOT NULL,
   PRIMARY KEY (`id`),
   KEY `user_id` (`user_id`)
)

采用无损发动机。

交易经常发生,因为有时可能是字母数字。

补贴是主要的关键。

因此,我有超过1M记录。 然而,有点要检查具体来源的重复转盘。 因此,我要问:

SELECT * 
  FROM UserTrans 
 WHERE transaction_id =  212398043  
   AND source =  COMPANY_A ;

this query getting very slow, like 2 seconds to run now. Should I index the transaction_id and the source?
e.g. KEY join_id (transaction_id, source)

如果有的话,有什么缺点?

最佳回答

明显的好处是,它将改善某些询问的绩效。

缺点是,为维持该指数,将花费一定空间储存该指数和为房舍管理处工作范围。 该指数特别容易耗用空间,因为你的交易如此广泛。

您可以考虑交易是否真正需要长到255个特点,或者如果你宣布交易最长期限为较短。

或您可使用prefix index,仅对第一个>>/em>进行索引。 性质:

CREATE INDEX join_id ON UserTrans (transaction_id(16), source(16));

@Daniel有一个很好的观点,即你可能获得同样的惠益,并且只把一个栏子索引,从而节省更多的空间。 既然你重读了<条码>。

另外,如果你打算交易——是独一无二的,为什么不限制交易是独一无二的?

CREATE UNIQE INDEX uq_transaction_id ON UserTrans (transaction_id(16));
问题回答

主要的反馈是,新指数将占用你的磁盘上的空间。 还将使插入和增补略微缓慢(但大多数情况下往往微不足道)。

另一方面,你的问询可能只停留在几个小秒,而不是两个秒。

增加指数的缺点是空间(因为储存指数确实占用了空间),并加插时间(因为当你插入新的记录时,必须将这些记录列入指数)。

尽管如此,你可能不需要将两个领域都列入索引——仅仅将其中的一个领域指数化就足够了。

I would think about diching your id column and use transaction_id as your primary key I am assuming that transaction_id is unique.

这意味着,你的计划阻止你插入已经存在的交易。

这减少了所储存的数据数量,也减少了需要编入索引的栏数。

如果来源公司和交易(id)没有混合钥匙。 i) 使两栏成为主要内容。

您目前的计划使你能够重复,这是不必要的罪恶。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...