------------------------------------- | user_id | user_name | user_visits | ------------------------------------- | 1 | foo | 5 | ------------------------------------- | 2 | bar | 12 | -------------------------------------
user_id:auto increament,user_visits:默认值1
INSERT INTO table(user_name)VALUES('baz'),('bar'),('qux');
上述声明当然会插入3条新记录,结果如下:
------------------------------------- | user_id | user_name | user_visits | ------------------------------------- | 1 | foo | 5 | ------------------------------------- | 2 | bar | 12 | ------------------------------------- | 3 | baz | 1 | ------------------------------------- | 4 | bar | 1 | ------------------------------------- | 5 | qux | 1 | ------------------------------------- …
我在两个表格A,B,C,D之间插入,相信我在A,B,C,D上创建了一个唯一索引以防止重复.但是我不知何故简单地在那些上做了一个正常的索引.所以插入了重复项.这是2000万的记录表.
如果我将现有索引从正常更改为唯一,或者只是为A,B,C添加新的唯一索引,D将删除重复项还是由于存在唯一记录而添加失败?我测试它是30密耳的记录,我不想弄乱桌子或复制它.
在比较检查列表是否包含R中的另一个列表时,比较两个函数的效率,我偶然发现了一个有趣的结果.排序大大提高了duplicated矢量大时的效率.这是一个惊喜,因为我从未注意到我自己的工作使用时有相当大的差异duplicated.事实上,对于我每天工作的尺寸,没有区别.注意:
set.seed(1007)
s1 <- sample(10^2, 10^3, replace = TRUE)
s1_sort <- sort(s1)
library(microbenchmark)
microbenchmark(dp=duplicated(s1), dp_sort=duplicated(s1_sort), times=1000)
Unit: microseconds
expr min lq mean median uq max neval cld
dp 16.459 16.9425 22.06371 17.2965 22.5050 1541.137 1000 a
dp_sort 17.007 17.5005 25.54953 17.8200 23.3655 1549.198 1000 a
Run Code Online (Sandbox Code Playgroud)
如您所见,向量排序时的时间没有明显差异.然而,在非常大的向量上,结果是非常不同的.注意:
s2 <- sample(10^6, 10^7, replace = TRUE)
s2_sort <- sort(s2)
microbenchmark(dp=duplicated(s2), dp_sort=duplicated(s2_sort), times=100)
Unit: milliseconds
expr min lq mean median uq max neval cld
dp 816.6883 847.9231 869.6829 …Run Code Online (Sandbox Code Playgroud) 我有一个具有许多行和列的矩阵
x <- matrix(c(1, 1, 3, 3, 55, 55, 1, 3, 3, 1,
1, 1, 3, 3, 55, 55, 1, 3, 9, 1), ncol = 2)
Run Code Online (Sandbox Code Playgroud)
在每组重复行(即每组相同的行)中,我希望标识第一行索引并将其分配给该组中的所有出现情况。例如,1两列(第 1、2、7、10 行)中都有多个重复行。在每一行上,我想要第一行索引,即 1。
x
# [,1] [,2]
# [1,] 1 1 # first row of 1-1. Assign its row index, 1, to all 1-1 rows
# [2,] 1 1
# [3,] 3 3 # first row of 3-3. Assign its row index, 3, to all 3-3 rows
# …Run Code Online (Sandbox Code Playgroud) 我正在使用IEnumerable's ToDictionary()扩展方法创建一个Dictionary对象:
var dictionary = new Dictionary<string, MyType>
(myCollection.ToDictionary<MyType, string>(k => k.Key));
Run Code Online (Sandbox Code Playgroud)
执行时,会抛出以下内容ArgumentException:
已添加具有相同键的项目.
如何让它告诉我重复的密钥是什么?
这是我的查询:
SELECT autor.entwickler,anwendung.name
FROM autor
left join anwendung
on anwendung.name = autor.anwendung;
entwickler | name
------------+-------------
Benutzer 1 | Anwendung 1
Benutzer 2 | Anwendung 1
Benutzer 2 | Anwendung 2
Benutzer 1 | Anwendung 3
Benutzer 1 | Anwendung 4
Benutzer 2 | Anwendung 4
(6 rows)
Run Code Online (Sandbox Code Playgroud)
我想为字段中的每个不同值保留一行name,并丢弃其他如下:
entwickler | name
------------+-------------
Benutzer 1 | Anwendung 1
Benutzer 2 | Anwendung 2
Benutzer 1 | Anwendung 3
Benutzer 1 | Anwendung 4
Run Code Online (Sandbox Code Playgroud)
在MySQL中,我会这样做:
SELECT autor.entwickler,anwendung.name
FROM autor …Run Code Online (Sandbox Code Playgroud) 我有一个表,包含,例如,我想在数据库中使两个字段唯一.例如:
create table Subscriber (
ID int not null,
DataSetId int not null,
Email nvarchar(100) not null,
...
)
Run Code Online (Sandbox Code Playgroud)
ID列是主键,DataSetId和Email都被编入索引.
我希望能够做的是阻止表中出现相同的Email和DataSetId组合,换句话说,对于给定的DataSetId,Email值必须是唯一的.
我尝试在列上创建一个唯一索引
CREATE UNIQUE NONCLUSTERED INDEX IX_Subscriber_Email
ON Subscriber (DataSetId, Email)
Run Code Online (Sandbox Code Playgroud)
但我发现这对搜索时间产生了相当大的影响(例如,当搜索电子邮件地址时 - 表中有150万行).
有没有更有效的方法来实现这种类型的约束?
我很难搞清楚这一点,并想知道是否有人能指出我正确的方向......
从这个清单:
N = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建:
L = [[1],[2,2],[3,3,3],[4,4,4,4],[5,5,5,5,5]]
Run Code Online (Sandbox Code Playgroud)
发现相同的任何值都被分组到它自己的子列表中.这是我到目前为止的尝试,我想我应该使用while循环?
global n
n = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5] #Sorted list
l = [] #Empty list to append values to
def compare(val):
""" This function receives index values
from the n list (n[0] etc) """
global valin
valin = val
global count
count = 0
for i in xrange(len(n)):
if valin == n[count]: # If the input value i.e. n[x] == n[iteration]
temp = valin, n[count]
l.append(temp) #append the values to a …Run Code Online (Sandbox Code Playgroud) 我使用脚本来删除mongo上的重复项,它在我用作测试的10个项目的集合中工作但是当我使用600万个文档的真实集合时,我收到错误.
这是我在Robomongo(现在称为Robo 3T)中运行的脚本:
var bulk = db.getCollection('RAW_COLLECTION').initializeOrderedBulkOp();
var count = 0;
db.getCollection('RAW_COLLECTION').aggregate([
// Group on unique value storing _id values to array and count
{ "$group": {
"_id": { RegisterNumber: "$RegisterNumber", Region: "$Region" },
"ids": { "$push": "$_id" },
"count": { "$sum": 1 }
}},
// Only return things that matched more than once. i.e a duplicate
{ "$match": { "count": { "$gt": 1 } } }
]).forEach(function(doc) {
var keep = doc.ids.shift(); // takes the first _id …Run Code Online (Sandbox Code Playgroud) 在mysql表中info我有:
ID,姓名,城市,日期,状态
我想从"info"中选择所有名称进行查询
$query = mysql_query("SELECT name FROM info WHERE status = 1 ORDER BY id")
or die(mysql_error());
while ($raw = mysql_fetch_array($query))
{
$name = $raw["name"];
echo ''.$name.'<br>';
}
Run Code Online (Sandbox Code Playgroud)
好吧,结果是它返回所有条目.我想回复所有条目没有重复.
说:在原始的"名称"下,我们插入了名字"约翰"10次.
我想只回应一次.这可能吗?
duplicates ×10
mysql ×4
r ×2
select ×2
sql ×2
append ×1
c# ×1
dictionary ×1
field ×1
indexing ×1
insert ×1
list ×1
matrix ×1
mongodb ×1
performance ×1
postgresql ×1
python ×1
sorting ×1
sql-server ×1
unique-index ×1