2012-12-02 29 views
0

我正在使用Twitter流API创建一个包含PHP和MySQL的推文数据库。随着“推文”表,有单独的表来跟踪提及,网址,主题标签和用户。MySQL查询删除不再相关的数据

我有它定期检查,只保留最近的1000个关于我的每个关键字的鸣叫,从数据库中删除其余的。我想要做的是清理关联表中的数据(通过关联表,我的意思是提及,网址,标签和用户表)。

我想知道什么是最有效的方式来删除所有提及,网址,标签和用户在“tweets”表中没有匹配的tweet_id将是。

表结构:

-- 
-- Table structure for table `tweets` 
-- 

CREATE TABLE IF NOT EXISTS `tweets` (
    `tweet_id` bigint(20) unsigned NOT NULL, 
    `tweet_text` varchar(200) NOT NULL, 
    `entities` text NOT NULL, 
    `created_at` datetime NOT NULL, 
    `geo_lat` decimal(10,5) DEFAULT NULL, 
    `geo_long` decimal(10,5) DEFAULT NULL, 
    `user_id` int(10) unsigned NOT NULL, 
    `screen_name` char(20) NOT NULL, 
    `name` varchar(40) DEFAULT NULL, 
    `profile_image_url` varchar(200) DEFAULT NULL, 
    `tweet_keywords` varchar(128) NOT NULL, 
    PRIMARY KEY (`tweet_id`), 
    KEY `created_at` (`created_at`), 
    KEY `user_id` (`user_id`), 
    KEY `screen_name` (`screen_name`), 
    KEY `name` (`name`), 
    FULLTEXT KEY `tweet_text` (`tweet_text`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `tweet_mentions` 
-- 

CREATE TABLE IF NOT EXISTS `tweet_mentions` (
    `tweet_id` bigint(20) NOT NULL, 
    `source_user_id` bigint(20) NOT NULL, 
    `target_user_id` bigint(20) NOT NULL, 
    KEY `tweet_id` (`tweet_id`), 
    KEY `source` (`source_user_id`), 
    KEY `target` (`target_user_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `tweet_tags` 
-- 

CREATE TABLE IF NOT EXISTS `tweet_tags` (
    `tweet_id` bigint(20) NOT NULL, 
    `tag` varchar(100) NOT NULL, 
    KEY `tweet_id` (`tweet_id`), 
    KEY `tag` (`tag`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `tweet_urls` 
-- 

CREATE TABLE IF NOT EXISTS `tweet_urls` (
    `tweet_id` bigint(20) NOT NULL, 
    `url` varchar(140) NOT NULL, 
    KEY `tweet_id` (`tweet_id`), 
    KEY `url` (`url`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `users` 
-- 

CREATE TABLE IF NOT EXISTS `users` (
    `user_id` bigint(20) unsigned NOT NULL, 
    `screen_name` varchar(20) NOT NULL, 
    `name` varchar(40) DEFAULT NULL, 
    `profile_image_url` varchar(200) DEFAULT NULL, 
    `location` varchar(30) DEFAULT NULL, 
    `url` varchar(200) DEFAULT NULL, 
    `description` varchar(200) DEFAULT NULL, 
    `created_at` datetime NOT NULL, 
    `followers_count` int(10) unsigned DEFAULT NULL, 
    `friends_count` int(10) unsigned DEFAULT NULL, 
    `statuses_count` int(10) unsigned DEFAULT NULL, 
    `time_zone` varchar(40) DEFAULT NULL, 
    `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    PRIMARY KEY (`user_id`), 
    KEY `user_name` (`name`), 
    KEY `last_update` (`last_update`), 
    KEY `screen_name` (`screen_name`), 
    FULLTEXT KEY `description` (`description`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 
+1

需要看表结构 – 2012-12-02 19:11:35

+0

OK - 添加了表结构 – unsunghero

回答

0

看一看外键约束。

+0

我使用Google搜索并找到了这篇文章:http://bradmontgomery.blogspot.ca/2009/04/how-to-set-up-foreign-key -constraint-in.html。不会使用外键约束只是在我的tweets表中添加一个限制,以便在不首先删除所有关联的数据的情况下不能删除tweet?我只是想在删除推文时自动删除关联的数据。 – unsunghero

+0

啊,明白了。我将我的引擎更改为InnoDB,并通过“ON UPDATE CASCADE”和“ON DELETE CASCADE”添加外键约束。很棒。 – unsunghero