设计问题。 [英] A Question of design.

查看:62
本文介绍了设计问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我的问题更多是关于脚本设计的建议。我在我的数据库中有大约

3600个条目,用户提交了一个列表,然后根据数据库中的那些条目检查
以确认它们是否是

已拥有特定商品。如果他们这样做,那么它就不会被添加到

用户表中,如果是,则将_is_添加到用户表中。

但是,如果item不在数据库中,建议用户使用这个
。所以基本上,我需要找出一种快速方法来比较

用户提交的项目(可能是50到700项),以及我创建的数组中的那些$ / b $ b使用数据库中的项目。


我可以想到两种方法来实现这一目标。首先,我可以遍历

所有用户项目,并使用in_array()查看它们是否在

数据库数组中。我认为我可以使用的另一种方法,非常类似于
,但不是有一个数据库项目数组,我可以将它们全部放入一个逗号分隔的字符串中,并遍历

用户项数组,使用正则表达式检查项目是否在

数据库中。可能有另一种更有效的方法来实现我正在寻找的结果,但我想不出其他任何事情。


我会很感激如果有人能告诉我哪两个可能更快,或者即使有一个更好的方式,也可以告诉我。我需要

来找到最快捷的方式,因为我不想过度使用服务器或

进行处理导致服务器超时。


一切顺利。


Daz。

Hello all,

my question is more regarding advice on a script design. I have about
3600 entries in my database, the user submits a list, which is then
checked against those in the database to confirm whether or not they
already own a particular item. If they do, then it''s not added to the
user table, whereas if it is, then it _is_ added to the user table.
However, if the item is not in the database, the user is advised of
this. So basically, I need to figure out a quick way to compare the
users submited items (probably 50 to 700 items), with those in an array
that I have created using the items from the database.

I can think of two ways to achieve this. Firstly, I can iterate through
all of the users items, and use in_array() to see if they are in the
database array of items. I think another method I can use, is very
similar, but rather than have an array of database items, I can put
them all into a single comma seperated string, and iterate through the
array of user items, using regex to check if the item is in the
database. There may be another more efficient way to acheive the
results I am looking for, but I can''t think of anything else.

I would appreciate it if anyone could tell me which of the 2 is likely
to be faster, or even if there is an even better way altogether. I need
to find the quickest way, as I don''t want to over work the server or
for the processing to cause a server timeout.

All the best.

Daz.

推荐答案

Daz写道:
Daz wrote:

我可以想到两种方法来实现这一目标。首先,我可以遍历

所有用户项目,并使用in_array()查看它们是否在

数据库数组中。我认为我可以使用的另一种方法,非常类似于
,但不是有一个数据库项目数组,我可以将它们全部放入一个逗号分隔的字符串中,并遍历

用户项数组,使用正则表达式检查项目是否在

数据库中。可能还有另一种更有效的方法来实现我正在寻找的结果,但我无法想到其他任何事情。
I can think of two ways to achieve this. Firstly, I can iterate through
all of the users items, and use in_array() to see if they are in the
database array of items. I think another method I can use, is very
similar, but rather than have an array of database items, I can put
them all into a single comma seperated string, and iterate through the
array of user items, using regex to check if the item is in the
database. There may be another more efficient way to acheive the
results I am looking for, but I can''t think of anything else.



通常,您会将唯一条件的强制执行留给

数据库,以避免竞争条件。如果相关列有索引,数据库还可以更快地找到现有记录。

Typically you would leave the enforcement of unique conditions to the
database, in order to avoid race conditions. The database can also more
quickly find an existing record if the column in question has an index.




Chung Leong写道:

Chung Leong wrote:

Daz写道:
Daz wrote:

我能想到两种方法实现这一点。首先,我可以遍历

所有用户项目,并使用in_array()查看它们是否在

数据库数组中。我认为我可以使用的另一种方法,非常类似于
,但不是有一个数据库项目数组,我可以将它们全部放入一个逗号分隔的字符串中,并遍历

用户项数组,使用正则表达式检查项目是否在

数据库中。可能还有另一种更有效的方法来实现我正在寻找的结果,但我无法想到其他任何事情。
I can think of two ways to achieve this. Firstly, I can iterate through
all of the users items, and use in_array() to see if they are in the
database array of items. I think another method I can use, is very
similar, but rather than have an array of database items, I can put
them all into a single comma seperated string, and iterate through the
array of user items, using regex to check if the item is in the
database. There may be another more efficient way to acheive the
results I am looking for, but I can''t think of anything else.



通常,您会将唯一条件的强制执行留给

数据库,以避免竞争条件。如果相关列有索引,数据库还可以更快地找到现有记录。


Typically you would leave the enforcement of unique conditions to the
database, in order to avoid race conditions. The database can also more
quickly find an existing record if the column in question has an index.



Hi Chung。


问题是当我有几百个结果要比较时。

我应该多次查询数据库吗?我可以用

a单一查询来完成,如果是这样,我怎么知道用户已经拥有某项特定项目的项目,并使用PHP更新数据库MySQL

图层。根据我的理解,你不能在SELECT语句中执行和更新或插入
语句。你也不能在1中执行几个

语句,例如多个UPDATE语句或几个INSERT

语句。


我知道你可以使用INSERT INTO table_name VALUES(''val1'',''val2''),

(''val3'',''val4'')...;

但是这个查询是行不通的,特别是在一个选择的

语句中:

Hi Chung.

The problem is that when I have a few hundred results to compare.
Should I really query the database that many times? Could I do it with
a single query, and if so, how would I know what items the user already
owns a particular item, and update the database using the PHP-MySQL
layer. To my understanding, you can''t execute and UPDATE or INSERT
statement from within a SELECT statement. Nor can you execute several
statements, such as multiple UPDATE statements or several INSERT
statements all in 1.

I know you could use INSERT INTO table_name VALUES (''val1'',''val2''),
(''val3'',''val4'')...;
But this query wouldn''t work, especially from within a select
statement:


query =" INSERT INTO table_name VALUES(''val1'',''val2''); INSERT INTO

table_name VALUES(''val3'',''val4'');" ;;


这是我希望修复的内容虽然我确信有一个完全正确的理由,因为我们都知道通过phpmyadmin执行

,或者通过CLI,它会工作很好。


希望你能看出我的问题所在。简单来说,它是

本质上如何:

a)查找用户列表中有效的项目(在数据库中)

然后:

1)如果需要添加它



2)让用户知道他们已经拥有它。

AND

b)找到用户列表中的项目,不在数据库中(如果

any),并让他们知道。


我希望这是有道理的。


非常感谢你。输入。


Daz

query = "INSERT INTO table_name VALUES (''val1'',''val2''); INSERT INTO
table_name VALUES (''val3'',''val4'');";

It''s something I wish was fixed, although I am sure there is a
perfectly valid reason for it not to be, as we both know executing
these through phpmyadmin, or through the CLI, it would work fine.

Hopefully you can see where my problem is. Just to recap, it''s
essentially how to:
a) Find the items in the users list that are valid (in the database)
and then:
1) Add it if needed
OR
2) Let the user know they already have it.
AND
b) Find the items in the users list tht aren''t in the database (if
any), and let them know.

I hope this makes sense.

Many thanks for you.r input.

Daz


这篇关于设计问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆