Php Mysql查询性能或逻辑改进

时间:2015-01-02 19:27:49

标签: php mysql performance

完全没有想法,可能需要一个我看不到的简单解决方案..我需要一个更好的逻辑,我正在做什么或一些性能提示。谢谢你,对不起我的英语。

好的,这就是我要做的事情: 我有一些订单(表comenzi),每个订单都有一个或多个产品(表croit),其中一些产品有库存(表magazie)。

我想显示如下列表: 订单1有产品1,但产品1也可以在其他订单上找到, 所以对于每个订单,我搜索它拥有的产品, 对于该订单中的每个产品,我搜索所有其他订单,看看我是否找到它, 如果我发现它我搜索库存以显示我们库存的那些产品或类似产品中有多少,之后我需要排除该产品,所以当我到达包含该特定产品的订单时,它将不再显示在他的命令。 这是一个简单的例子:

O1 (Order 1 from table comenzi) has P1,P2,P3 (products for Order 1 in table croit ),
O2 has P2,P5,P7
O3 has P1,P2,P7


The output should be:
O1 (Order 1 name and details)
    +P1 ( O1 - some details including stock)
    -P1 ( O3 - some details including stock)
    +P2 ( O1 - some details including stock)
    -P2 ( O2 - some details including stock)
    -P2 ( O3 - some details including stock)
    +P3 ( O1 - some details including stock)
O2 (Order 2 name and details)
    +P5 ( O2 - some details including stock)
    +P7 ( O2 - some details including stock)
    -P7 ( O3 - some details including stock)
O3 (Order 3 name and details)
    - no products because they were already displayed before

如您所见+符号表示产品来自该订单,首先出现,然后是其他订单的产品。 这是我想要的基本输出。 如果您需要更多信息,请询问。

我在:http://sqlfiddle.com/#!2/89147/1

制作了一个方形小册子

这里是索引,由于某些原因,当我使用它们时,小提琴给我一个错误..

--
-- Indexes for table `comenzi`
--
ALTER TABLE `comenzi`
ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `id` (`id`);

--
-- Indexes for table `croit`
--
ALTER TABLE `croit`
ADD PRIMARY KEY (`id`);

--
-- Indexes for table `croitconf`
--
ALTER TABLE `croitconf`
ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `codprod` (`codprod`);

--
-- Indexes for table `magazie`
--
ALTER TABLE `magazie`
ADD PRIMARY KEY (`id`);

--
-- Indexes for table `magazine`
--
ALTER TABLE `magazine`
ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `comenzi`
--
ALTER TABLE `comenzi`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=10313;
--
-- AUTO_INCREMENT for table `croit`
--
ALTER TABLE `croit`
MODIFY `id` int(10) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=87501;
--
-- AUTO_INCREMENT for table `croitconf`
--
ALTER TABLE `croitconf`
MODIFY `id` int(10) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2106;
--
-- AUTO_INCREMENT for table `magazie`
--
ALTER TABLE `magazie`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2794;
--
-- AUTO_INCREMENT for table `magazine`
--
ALTER TABLE `magazine`
MODIFY `id` int(10) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=83;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

为了简单起见,我将在此处仅添加sql查询以及有关它们应如何工作的一些信息:

查询1:加载订单列表并将id保存为数组,同时我将数组保存在$ _SESSION中以进行分页,

$query = "SELECT numar_comanda FROM comenzi where factura ='' and valoare = '' and dataplecare!='NULL' and factcorectie = '' and terminat = 0";

和php:

while ($rowcom=mysqli_fetch_array($sqlcom)) {
    $combun[$rowcom['numar_comanda']]=$rowcom['numar_comanda'];
}
$_SESSION[combun]=$combun;

查询2:基本上是与具有额外字段的查询相同,但这次是针对每个订单,我开始拉动产品在我的订单下显示

$query = "SELECT comenzi.numar_comanda, comenzi.dataplecare as dataplecare, comenzi.data_emitere as dataemitere, magazine.nume as magazin FROM comenzi,magazine
where comenzi.magazine_id=magazine.id and comenzi.factura ='' and comenzi.valoare = '' and comenzi.dataplecare!='NULL' and comenzi.factcorectie = ''  and comenzi.terminat = 0
order by comenzi.dataplecare ASC "; 

//我有一个LIMIT $ postnumbers OFFSET $ offset但不是必需的,如果我能设法让它加载快,我可以删除分页

和php

while ($row=mysqli_fetch_assoc($sqlq)) {
//echo a row with order details
if ($_SESSION[prod] != "") $donecodfurn="and codfurn not in(" . implode(",", array_filter($_SESSION[prod])) . ")"; //add explude rule to the next query so that the products that i already used will be dropped

在这个内部,我有下一个查询从加载命令加载所有产品并循环它们:

$query = "SELECT croitconf.codprod as codprod,croitconf.codfurn as codfurn,croitconf.denumire as denumire,
croit.id as croitid,croit.croitconf_id as croitconfid,croit.cantitate as cantitate,croit.stare as stare
FROM croit,croitconf where croit.croitconf_id = croitconf.id and croit.stare <= 3 and croit.comenzi_id=$row[numar_comanda] $donecodfurn order by codfurn asc";

和php:

$sqlcomanda=mysqli_query($sql, $query) or die(mysqli_error($sql));
$codfurn="";
while ($com=mysqli_fetch_assoc($sqlcomanda)) { //for every product of this order
    //echo a row with the product details and other info

在这个循环中我从表magazie获得信息,这是一个股票表,这有2个选项,如果我们有库存的确切产品(相同的codprod)或类似的产品(相同的codfurn)

$query =  "select sum(cantitate) as cant from magazie where codfurn = '" . $com[codfurn] . "'";

如果我们在库存展示中有类似的产品

$magq = mysqli_query($sql,$query);
$mag=mysqli_fetch_array($magq);
//echo some data about stock if $mag is array


$query = "select sum(cantitate) as cant from magazie where codprod  = '" . $com[codprod] . "'";

php如果我们在库存显示器中有相同的产品

$magq=mysqli_query($sql, $query);
$mag=mysqli_fetch_array($magq);
//echo some data about stock similar if $mag is array

进一步我看看我的数组,看看我是否还有未经处理的订单来获得与当前产品相同的产品

if (count($_SESSION[combun]) > 0) {

$donecom="and croit.comenzi_id IN(" . implode(",", array_filter($_SESSION[combun])) . ")"; // create a rule to search only in remaining orders

$query = "select croitconf.codprod as codprod,croitconf.codfurn as codfurn,croitconf.denumire as denumire,
croit.id as croitid,croit.comenzi_id as numar_comanda,croit.croitconf_id as croitconfid,croit.cantitate as cantitate,croit.stare as stare
FROM croit,croitconf where croit.croitconf_id = croitconf.id and croit.stare <= 2 and croitconf.codfurn=$com[codfurn] $donecom and croit.comenzi_id != '$row[numar_comanda]' order by croitconf.codprod asc";

以及在$ _SESSION

中显示和修改数组的php
$produse= mysqli_query($sql, $query);
while ($prod=mysqli_fetch_assoc($produse)) { //similar products as the one from the initial order
//echo something about the curent product, order number and some other infos

//i look again in our stock to see if we have this or a similar product, so basicaly the same code when we first look

    $query =  "select sum(cantitate) as cant from magazie where codfurn = '" . $com[codfurn] . "'";
    //if we find similar echo something
    $query = "select sum(cantitate) as cant from magazie where codprod  = '" . $com[codprod] . "'";
    //if we find same echo something    
} // end similat products
$_SESSION[prod][$com[codfurn]]=$com[codfurn]; // add product in array so we don't process it again
} 
} //end curent product from curent order
unset($_SESSION['combun'][$row['numar_comanda']]); //remove order from array so we won't search for products in it

} //end curent order

0 个答案:

没有答案