是一个电影程序。
其中一个films表是这样设计的
film_id   film_name   u_include    u_exclude(中间一些列省略,主要是最后两个的问题)
 1        功夫        [2][3]        null
 2        大话西游    null          null
 3        地道战      null          [5][7]其中[2][3][5][7]都是用户表中的用户ID,
u_include  如果为null(空)则表示这部电影是公众电影,也就是所有用户都可以显示,如果有用户编号则只能让指定的用户显示
u_exclude  如果为空则没有特殊意义,如果有用户编号则表示这个用户不显示这个电影。也就是排除这个电影在指定的用户上显示。用户数不多。200个左右,但是访问量应该是用户数的100倍左右,因为是根据IP来判断用户,而每个IP内部访问这个页面都是属于这个用户的。
我的初步想法是用大概如下的查询字符串来针对用户的数据库读取
Select * from [films] Where [film_name] = 影片名 and (u_include like &[编号]& or u_include = null) and (u_exclude = null or film_id not in (select film_id form films where u_exclude like &[编号]&))
查询语句没有做测试,不知道是否可行。
但是我怕的是由于用到了like查询,并且不止一次,而且还有排除查询等。应该数据库开销比较大把?
其实由于是电影程序。数据量不会很多。最多几千条记录,但是如果我用这样的查询。如果访问量稍多。是否会严重影响性能??不知道大家有什么好的方法没有?

解决方案 »

  1.   


    Select * from [films] Where [film_name] = 影片名 and (u_include like &[[]编号]& or u_include is null) and (u_exclude is null or u_exclude not like &[[]编号]&)
      

  2.   

    楼主的担忧不是没有道理的,这样的处理的确不是一个程序高手所为,建议楼主还是使用数据库范式,新建一个表user_film( uniqueid  userid  film_id ) 这样在用户登录时只须从这个表选择所有此用户的记录 ,对于公众电影则可使用一点小技巧来处理 ,建一个public用户 所有公众电影 都划给他 这样查询语句将是这样:select film_id ,  film_name 
    from films fs, user_film uf
    where fs.film_id=uf.film_id 
    and (uf.user_id=& or uf.user_id= public_id)
      

  3.   

    谢谢楼上的两位。我打算放弃原来的这种结构。单独建立一个include 和exclude 表。
    icnlude表大致如下:
    id   user_id   films_id
    exclude表也和上面一样films表中去掉以上两个字段。但是加上 is_private 字段表示这部电影不是公众的
    因为影片默认是公众的。而且用户数随时可能增加或者减少。不能吧所有影片权限都方在icnlude上。这样icnlude会月来越庞大,我觉得。。这样做应该如何设计查询才能达到pcplayboy()的这种语句的效果呢?
    Select * from [films] Where [film_name] = 影片名 and (u_include like &[[]编号]& or u_include is null) and (u_exclude is null or u_exclude not like &[[]编号]&)我想大概是 select * form films where film-name = 影片查询名 and is_private = false or  film_id in (select film_id form include where user_id = 用户编号) and film_id not in (select film_id form exclude where user_id = 用户编号)但是这样的查询。是否会消耗数据库资源呢?因为有两个排除查询