我正在尝试执行mysql查询以显示推荐列表。按朋友的邻近度排序的推荐列表。我能够根据朋友的推荐对列表进行排序,但我无法根据朋友的朋友对推荐列表进行排序。
以下是表格的设计:
/*Table structure for table `recommendation` */
CREATE TABLE `recommendation` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`kategori` varchar(255) NOT NULL,
`nama` varchar(255) NOT NULL,
`rating` set('1','2','3','4','5') NOT NULL,
`writer_id` varchar(255) NOT NULL,
`search_here` longtext NOT NULL,
`user_type` set('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `kategori` (`kategori`),
KEY `nama` (`nama`),
FULLTEXT KEY `search_here` (`search_here`)
)
ENGINE=MyISAM AUTO_INCREMENT=100174
DEFAULT CHARSET=latin1 COMMENT='recommendation table';
/*Table structure for table `user_relationship` */
DROP TABLE IF EXISTS `user_relationship`;
CREATE TABLE `user_relationship` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`me` varchar(255) NOT NULL,
`friend` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `me` (`me`)
)
ENGINE=MyISAM AUTO_INCREMENT=100982
DEFAULT CHARSET=latin1 COMMENT='user relationship table';
我应用以下查询:
SELECT
a.id AS ids,
a.writer_id,
a.kategori,
a.rating,
b.me,
a.user_type
FROM
recommendation a
LEFT OUTER JOIN
user_relationship b
ON
a.writer_id = b.friend
AND
b.me='$USER_ID'
WHERE
MATCH(search_here) AGAINST('$SEARCH' IN BOOLEAN MODE)
ORDER BY
b.me DESC,
a.writer_id
建议清单应按以下顺序显示:朋友推荐,朋友朋友推荐以及公众推荐。
谁能帮帮我? 感谢编辑:
我尝试过创建名为“user_friend_friend”的user_relationship表的克隆。 “user_friend_friend”表设计如下:
CREATE TABLE `user_friend_friend` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`friend` varchar(255) NOT NULL,
`friend2` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `friend` (`friend`)
)
ENGINE=MyISAM AUTO_INCREMENT=100982
DEFAULT CHARSET=latin1 COMMENT='user friend friend table';
我执行以下查询:
SELECT
a.id AS ids,
a.writer_id,
a.kategori,
a.nama,
a.rating,
b.me, b.friend,
c.friend2 AS ff, a.user_type
FROM recommendation a
LEFT OUTER JOIN webref.user_relationship b
ON a.oleh = b.friend AND b.me='1010147270'
LEFT OUTER JOIN webref.user_friend_friend c
ON a.oleh = c.friend AND b.friend IS NULL
WHERE MATCH(search_here) AGAINST('$SEARCH' IN BOOLEAN MODE)
GROUP BY a.id
ORDER BY
b.me DESC,
user_type DESC,
rating DESC,
c.friend DESC,
a.writer_id
还有更有效的解决方案吗?
答案 0 :(得分:1)
一些性能提示: - 您应该在要加入的列上有索引。例如,它看起来不像writer_id有索引。 - 即使使用索引,使用VARCHAR(256)作为要加入的列的类型也很慢。将id作为整数存储会更好。你可以有一个单独的表来存储int - >您稍后查询的varchar映射。
但要做朋友查询的朋友,这样的事情应该有效:
SELECT
a.id AS ids,
a.writer_id,
a.kategori,
a.rating,
b.me,
a.user_type
FROM
recommendation a
LEFT OUTER JOIN
user_relationship b
ON
a.writer_id = b.friend
AND
b.me='$USER_ID'
LEFT OUTER JOIN
user_relationship c
ON
a.writer_id = c.friend
AND
c.me = b.friend