我有以下表格:
table_categories - id, name, imagepath
table_subcategories - sub_categories_id, sub_category_name
table_subcategory_categories - subcategory_id, category_id (Mapping table for category and sub category)
我想以这种格式获取所有类别及其各自的子类别:
[
{
"subcategories": [
{
"scname": "Sugar",
"scid": "5"
},
{
"scname": "Salt",
"scid": "6"
},
{
"scname": "Jaggery",
"scid": "7"
}
],
"name": "Salt and Sugar",
"id": "1",
"image": "/images/salt_sugar.png"
},
{
"subcategories": [
{
"scname": "Tea",
"scid": "8"
},
{
"scname": "Coffee",
"scid": "9"
},
{
"scname": "Tea Bags",
"scid": "162"
}
],
"name": "Tea and Coffee",
"id": "2",
"image": "/images/tea_and_coffee.png"
}
]
这是我获取类别和子类别的逻辑。
my $sql_query = "SELECT * from table_categories";
my $statement = $db_handle->prepare ($sql_query) or die "Couldn't prepare query '$sql_query': $DBI::errstr\n";
$statement->execute() or die "SQL Error: $DBI::errstr\n";
my @loop_data = ();
while (my @data = $statement->fetchrow_array()) {
my $sql = "SELECT SCC.subcategory_id, SC.sub_category_name
FROM
table_subcategory_categories as SCC
INNER JOIN
table_subcategories as SC
ON
SCC.subcategory_id = SC.sub_categories_id
where SCC.category_id = '$data[0]' ";
my $stmt = $db_handle->prepare ($sql) or die "Couldn't prepare query '$sql_query': $DBI::errstr\n";
$stmt->execute() or die "SQL Error: $DBI::errstr\n";
my @InnerLoopData = ();
while (my @InnerData = $stmt->fetchrow_array()) {
my %InnerData = ('scid', $InnerData[0], 'scname', $InnerData[1]);
push(@InnerLoopData, \%InnerData);
}
my %data = ('id', $data[0], 'name', $data[1], 'image', $data[2], 'subcategories', \@InnerLoopData);
push(@loop_data, \%data);
}
my $json_text = to_json(\@loop_data);
print $json_text;
$db_handle->disconnect;
每件事都按预期工作,但问题是因为内部查询响应太慢。由于我有超过1000个类别,内部查询执行超过1000次。
可以请任何人帮我如何优化此查询??
答案 0 :(得分:3)
您可以在perl脚本中进行处理,减少数据库调用次数。数据库调用很昂贵。
SELECT * FROM table_categories ORDER BY id ASC 将值存储在多维数组中。说类别
按category_id顺序选择count(*)FROM table_subcategory_categories group by class_id ASC
SELECT sc。*,scc.category_id来自table_subcategories sc,table_subcategory_categories scc WHERE sc.sub_categories_id = scc.sub_categories_id ORDER BY scc.category_id ASC
然后
my @loop_data = ();
my $currentPos = 0;
my $noOfElementsInThisCategory = 0;
foreach my $i (0 .. $#categories){
$noOfElementsInThisCategory = $category_count[$i];
my @InnerLoopData = ();
for (my $k = $currentPos; $noOfElementsInThisCategory > 0; $noOfElementsInThisCategory++) {
my %InnerData = ('scid', $sub_categories [$k][0], 'scname', $sub_categories [$k][1]);
push(@InnerLoopData, \%InnerData);
$currentPos = $currentPos + 1;
}
my %data = ('id', $categories[$i][0], 'name', $categories[$i][1], 'image', $categories[$i][2], 'subcategories', \@InnerLoopData);
push(@loop_data, \%data);
}
my $json_text = to_json(\@loop_data);
print $json_text;
答案 1 :(得分:2)
SCC.subcategory_id
SC.sub_categories_id
SCC.category_id
这个字段应该是索引,然后它会很快。联接应该在索引字段
上你也不需要(用于获取子类别数据)这里的循环(在循环中查询 - 坏主意),使用单个查询:
SELECT SCC.subcategory_id, SC.sub_category_name, C.*
FROM
table_subcategory_categories as SCC
INNER JOIN
table_subcategories as SC
ON
SCC.subcategory_id = SC.sub_categories_id
INNER JOIN table_categories as C ON SCC.category_id = C.id
答案 2 :(得分:0)
您应该使用placeholders and bind variables在循环外准备查询。
您还应该使用{ RaiseError => 1}
创建数据库句柄,这样您就不必明确检查错误。
您还应该将结果作为hashref获取,并避免每次循环时都创建自己的哈希值。
my $dbh = DBI->connect($dsn, { RaiseError => 1 });
my $sql1 = "SELECT * from table_categories";
my $sth1 = $dbh->prepare ($sql1);
$sth1->execute();
my @loop_data = ();
my $sql2 = qq/
SELECT SCC.subcategory_id as scid, SC.sub_category_name as scname
FROM table_subcategory_categories SCC
INNER JOIN table_subcategories SC
ON SCC.subcategory_id = SC.sub_categories_id
WHERE SCC.category_id = ?
/;
my $sth2 = $dbh->prepare( $sql2 );
while (my @data = $sth1->fetchrow_array()) {
$sth2->execute( $data[0] );
my @InnerLoopData = ();
while (my $InnerData = $sth2->fetchrow_hashref()) {
push(@InnerLoopData, $InnerData);
}
my %data = ( 'id' => $data[0],
'name' => $data[1],
'image' => $data[2],
'subcategories' => \@InnerLoopData );
push( @loop_data, \%data );
}
my $json_text = to_json(\@loop_data);
print $json_text;
$dbh->disconnect;