我应该使用自定义 Oracle 函数还是 PHP 来处理此 HTML 字符串构造?
在实现 这个问题关于我的项目,我意识到有 3 种方法可以完成某件事,但我不知道哪一种更好。是否会再次使用其中之一进行表演?这种性能值得更复杂的实现吗?这就是我想知道的事情。
尽管我没有具体说明,但该问题是关于从表中选择电影信息并将每个类型连接到以逗号分隔的单列中。现在,我需要这些类型在我的代码中是可点击的,这样我就可以按点击的类型进行过滤。
我想到了 3 种方法来做到这一点:
解决方案 1)
使用 1 个 SELECT 查询和其他问题上的函数,而不是使用 PHP 来构建 HTML 字符串。像这样的事情:
$genres = explode(',', $info['GENRES']);
$count = count($genres);
for($i = 0; $i < $count; $i++) {
$genres[$i] = '<a href="#">'.$genres[$i].'</a>';
}
$info['GENRES'] = implode(' | ', $genres);
然后我意识到我缺少流派 ID,因此我可以轻松创建一个链接以按该流派进行过滤,并且我考虑更改 Oracle 函数以包含它。如何?通过创建 JSON 字符串(即:{"1":"Action","2":"Crime","5":"Thriller"}
,可以轻松地为此修改该函数)相应地调整上面的代码很简单。
解决方案 2)
使用 2 个 SELECT 语句并完全忽略其他问题的函数。一个查询用于选择电影信息,另一个查询用于选择与该电影关联的所有类型(ID 和名称)。然后 PHP 会完成剩下的工作来构建 HTML 代码字符串。
解决方案 3)
只需使用 1 个 SELECT 查询和 Oracle 函数即可自行构造整个字符串。这意味着 PHP 代码就像执行 SQL 语句一样简单。 Oracle 函数将准备整个 HTML 字符串。
现在,这个解决方案可能无法像我真正需要的那样工作,使其变得无关紧要。但我不知道这一点,因为我不确定我将如何准确地构建这些 HTML 链接以按流派进行过滤,还没有真正考虑过。好吧,在 PHP 中我总是可以以某种方式做到这一点,但不确定我是否可以在 Oracle 函数中做同样的事情。
不过,让我们假设这是可能的,我相信它是可能的。
我应该采用哪种解决方案?
在这种情况下,是否有任何理由让 Oracle 函数完成所有工作(反之亦然)?鉴于此,我应该选择解决方案 3 还是解决方案 1/2?
如果解决方案 1/2 是更好的方法,我应该选择哪一种?解决方案 1 在 PHP 代码中有 1 个 SELECT,但在 Oracle 函数中有另一个。解决方案 2 在 PHP 代码中有 2 个 SELECT。其中一种是否比另一种有性能提升,或者它们基本相同?如果是的话,哪一个更好?
解决方案 1 有一个更简单的 PHP 代码,但为 Oracle 函数引入了稍微复杂的代码。另一方面,解决方案 2 根本没有 Oracle 函数代码,并且 PHP 代码的大小/复杂性会增加,但增加幅度不会很大。
我倾向于解决方案 1,但很想听听您的想法。
While implementing the code from this question on my project I realized there's 3 way I can accomplish something but I have no idea which one is better. Is there a performance again using one over the other? Is that performance worth the more complicated implementation? That's the kind of stuff I want to know.
Although I did not specify, that question was about selecting a movie info from a table and join every genre in a single column separated by commas. Now, I need those genres to be clickable in my code so I can filter by the clicked genre.
I thought of 3 ways to do this:
Solution 1)
Use 1 SELECT query and the function on that other question than use PHP to build the HTML string. Something like this:
$genres = explode(',', $info['GENRES']);
$count = count($genres);
for($i = 0; $i < $count; $i++) {
$genres[$i] = '<a href="#">'.$genres[$i].'</a>';
}
$info['GENRES'] = implode(' | ', $genres);
Then I realized I'm missing the genre ID so I can easily create a link to filter by that genre and I thought of changing the Oracle function to include it. How? By creating a JSON string (ie: {"1":"Action","2":"Crime","5":"Thriller"}
, the function can easily be modified for this) and it's a simple matter of adapting the code above accordingly.
Solution 2)
Use 2 SELECT statements and completely ignore the function on that other question. One query to select the movie info, the other to select all genres (id and name) associated to that movie. Then PHP would do the rest to build the HTML code string.
Solution 3)
Simply use 1 SELECT query and the Oracle function adapted to construct the whole string by itself. Meaning, the PHP code would be as simple as executing the SQL statement. The Oracle function would prepare the whole HTML string.
Now, this solution may not work as I really need it, rendering it irrelevant. But I don't know that cause I'm not sure how exactly I'm going to build those HTML links to filter by genre, haven't really thought about it yet. Well, in PHP I can always do it, one way or another, not sure I can do the same in an Oracle function.
Still, let's assume it's possible, I believe it is.
Which solution should I go for?
Is there any reason, in this situation, for the Oracle function to do all the work (or vice-versa)? Given this, should I go with solution 3 or solution 1/2?
If solution 1/2 is the better way, which one should I go for? Solution 1 has 1 SELECT in the PHP code but another in the Oracle function. Solution 2 has 2 SELECTs in the PHP code. Is there a performance gain on one over the other or they are basically the same? If so, which one is better?
Solution 1 has a simpler PHP code but introduces a slightly more complicated code for the Oracle function. In the other hand, solution 2 has no Oracle function code at all and the PHP code will increase in size/complexity, not by much though.
I'm inclined towards solution 1, but would love to hear your thoughts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我投票支持解决方案#2。是的,您将有更多的实际数据检索,但您的数据正在以更加规范化的方式使用。没有什么可以阻止您从第二次检索中创建数组并使用
explode()
创建逗号分隔列表。My vote is for solution #2. Yes, you will have more actual data retrievals, but your data is being used in a much more normalized manner. There's nothing stopping you from creating an array from the second retrieval and creating a Comma-Sep list with
explode()
.我可以建议一个完全不同的选择吗?
看起来您正在一对多的情况下查询数据。也就是说,电影记录具有一定数量的与其相关联的类型记录。我不确定 PHP(或驱动程序)是否支持这一点,但您的结果集本身可能包含指向另一个结果集的游标。想象一下如下的查询。请注意,我不知道你的表名是什么 - 我正在根据你的其他问题编造一些东西。
通过执行此操作,您将获得一个查询(对数据库的一次查询),该查询会返回您需要的所有数据。然后您可以让 PHP 生成 HTML,这就是它真正的用途。没有 PL/SQL 函数(这可能会严重影响性能),也不会对数据进行反规范化和重新规范化。
May I suggest a different option entirely?
It looks like you're querying data in a one-to-many situation. That is, a movie record has some number of genre records associated with it. I'm not sure if PHP (or the drivers) supports this, but your result set may itself contain a cursor to another result set. Imagine a query like below. Note that I have no idea what your table names are - I'm making stuff up based off your other question.
By doing this you get to have a single query (one hit to the database) which returns all the data you need. Then you can have PHP do the HTML generation, what it's really meant to do. No PL/SQL functions (which may severly impact performance) nor are you de-normalizing and re-normalizing data.