简体   繁体   English

在导入数据之前在 MySQL 数据库列上使用 php function

[英]Using a php function on a MySQL database column before importing data

I have been working with mySQL and PHP for a few months now and am building a search function for my website.我已经使用 mySQL 和 PHP 几个月了,我正在为我的网站构建搜索 function。 This search function has a filter and a search bar.这个搜索 function 有一个过滤器和一个搜索栏。 The filter is currently implemented by using the "WHERE" function of mySQL, by which I exclude any rows that don't fit within the filter requirements to avoid a large data transfer from mySQL.该过滤器目前是通过使用 mySQL 的“WHERE”function 来实现的,通过它我排除了任何不符合过滤器要求的行,以避免从 ZF8C0921E52FB066BDDD4D69E8DBBE70 传输大量数据。 I would like to do this for the search bar as well, but want to use PHP's levenshtein function between the title of the page and search term.我也想对搜索栏执行此操作,但想在页面标题和搜索词之间使用 PHP 的 levenshtein function。

Would anyone have any idea A. whether this is even possible and B. how this would be accomplished?有没有人知道 A. 这是否可能以及 B. 这将如何实现? Also maybe C.: should I even worry about the size of the database I'm transferring from mySQL?也可能是 C.:我是否应该担心从 mySQL 传输的数据库的大小? At what point does it cause serious latency for the user?它在什么时候会给用户带来严重的延迟?

An example of how I've done the filter implementation, which is automatically generated in a PHP function that is called by using AJAX.我如何完成过滤器实现的示例,它是在使用 AJAX 调用的 PHP function 中自动生成的。

SELECT title, coverphoto, highteavegatag, pagename, totaltimemin, totaltimemax, preptimemin, preptimemax, date FROM recipes WHERE ((LOCATE('french',cuisine) > 0) AND (LOCATE('dutch',cuisine) > 0)) AND ((dietglnpv & 2 = 2) AND (dietGLNPV & 4 = 4)) AND ((LOCATE('sweet',pagecategory) > 0) AND (LOCATE('diner',pagecategory) > 0)) AND ((totaltimemin <= 331) AND (totaltimemax >= 85)) AND ((preptimemin <= 45) AND (preptimemax >= 28))

And an idea of what I would like to add (I know this won't work, but maybe there's a way).以及我想添加的想法(我知道这行不通,但也许有办法)。

SELECT (...) FROM recipes WHERE (...) AND (levenshtein(searchstr,title) < 10)

I think this quickly will become very inefficient.我认为这很快就会变得非常低效。 Remember, you probably don't want to do a levenshtein() between the whole title and the search string, but between the words in the title and the words in the search string.请记住,您可能不想在整个标题和搜索字符串之间执行levenshtein() ,而是在标题中的单词和搜索字符串中的单词之间执行 levenshtein()。 The PHP manual isn't clear on this but levenshtein() computes the minimum number of single-character edits, not just edits of chunks of characters. PHP 手册对此并不清楚,但levenshtein()计算单字符编辑的最小数量,而不仅仅是字符块的编辑。

Most general search engines use a more sophisticated approach.大多数通用搜索引擎使用更复杂的方法。 You could use the full-text searching functionality in MySQL, or you could build your own system.您可以使用 MySQL 中的全文搜索功能,也可以构建自己的系统。 This usually means finding all the words that occur in the columns to be searched and indexing them.这通常意味着找到要搜索的列中出现的所有单词并将它们编入索引。 This indexes is done before any searches occur, that way each individual search doesn't have to look through all the texts.该索引是在任何搜索发生之前完成的,这样每个单独的搜索就不必查看所有文本。 Then you match the search string with these words and from them you find the matching rows.然后将搜索字符串与这些单词匹配,并从中找到匹配的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM