SQLITE用于表的每一行,计算两个表中的出现次数

时间:2016-08-24 09:03:43

标签: sqlite

我需要一些帮助来简化以下请求,并使其更快地执行。这是我正在尝试做的事情:

表播放器(idPlayer,namePlayer) 包含播放器列表

表EventPVE(idPlayer) 每一行代表玩家被游戏环境杀死的事件

表EventPVP(idPlayerKilled,idPlayerKiller) 每一行代表玩家杀死或被其他玩家杀害的事件

我正在尝试为每个玩家计算他们被环境杀死的次数,他们被玩家杀死的次数以及杀死其他玩家的次数。下面的请求有效但速度很慢(当然是3个子请求)

你能帮我简化一下吗?我在sql上非常糟糕....我已经看到了LEFT JOIN的一些“解决方案”,但似乎它在我的情况下不起作用。第一个LEFT JOIN运行良好,但其他LEFT JOIN返回错误计数。

SELECT  p.idPlayer, p.namePlayer,
        (
        SELECT  COUNT(*) 
        FROM    EventPVE pve
        WHERE   pve.idPlayer = p.idPlayer
        ) AS pveDeath,
         (
        SELECT  COUNT(*)
        FROM    EventPVP pvp
        WHERE   pvp.idPlayerKiller = p.idPlayer
        ) AS pvpKills,
         (
        SELECT  COUNT(*)
        FROM    EventPVP pvp
        WHERE   pvp.idPlayerKilled = p.idPlayer
        ) AS pvpDeaths
FROM    Player p

这是一个测试数据库:https://www.dropbox.com/s/tc7z1i26mumvhgm/test.db?dl=0

2 个答案:

答案 0 :(得分:0)

查询本身已经尽可能简单。

问题是数据库必须扫描所有表以查找具有匹配的玩家ID值的行,并且必须为每个玩家执行一次。

这些查找可以加速一些索引:

public class MovieAdapter extends ArrayAdapter {
    private List<CarrotModel> carrotModelList;
    private int resource;

    private LayoutInflater inflater;

    public MovieAdapter(Context context, int resource, List<CarrotModel> objects) {
        super(context, resource, objects);
        carrotModelList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder=null;
        if (convertView == null) {
            holder=new ViewHolder();
            convertView = inflater.inflate(resource, null);

            holder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);

            holder.tvDiscount = (TextView) convertView.findViewById(R.id.tvDiscount);
           holder.tvmername = (TextView) convertView.findViewById(R.id.tvMerName);
            holder.tvDealDesc = (TextView) convertView.findViewById(R.id.tvDealDesc);
            holder.tvOfftoDis = (TextView) convertView.findViewById(R.id.tvOftoDis);
            holder.tvmeradd = (TextView) convertView.findViewById(R.id.tvmeradd);
            convertView.setTag(holder);
        }
        else {

            holder = (ViewHolder) convertView.getTag();
        }


        final ProgressBar progressBar;


        progressBar=(ProgressBar) convertView.findViewById(R.id.progressBar);


        ImageLoader.getInstance().displayImage(carrotModelList.get(position).getImageURL(), holder.ivIcon, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                progressBar.setVisibility(View.GONE);
            }
        });
        holder.tvDiscount.setText(carrotModelList.get(position).getDiscount() + "% OFF");
        holder.tvmername.setText(carrotModelList.get(position).getMerchantName());
        holder.tvDealDesc.setText(carrotModelList.get(position).getDealDescription());
        holder.tvOfftoDis.setText(carrotModelList.get(position).getOffersToDisplay());
        holder.tvmeradd.setText(carrotModelList.get(position).getMerchantAddress());

        Button Button1= (Button)  convertView.findViewById(R.id.button);

        Button1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent=new Intent(getApplicationContext(), SecondActivity.class);
                intent.putExtra("data",holder.tvmername.getText().toString());
                startActivity(intent);
            }

        });


        return convertView;
    }

    class ViewHolder{
       private ImageView ivIcon;
        private TextView tvDiscount;
        private   TextView tvmername;
        private TextView tvDealDesc;
        private TextView tvOfftoDis;
        private  TextView tvmeradd;

    }


}

答案 1 :(得分:-1)

我认为不需要第二张表EventPVE

试试这个: SELECT * FROM eventpvp通过idPlayerKilled在eventpvp.idPlayerKilled = player.idPlayer group上左加入播放器

enter image description here

这对我有用。我没有使用你提供的数据库,我创建了一个。