为什么我的解决方案如此之慢以及如何提高查询的性能? [英] Why is my solution so slow and how can I improve performance of the query?

查看:183
本文介绍了为什么我的解决方案如此之慢以及如何提高查询的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我能够优化性能,但仍然有些缓慢:/ b / b / b / b / b / b / b / p最后编辑:



我当前的解决方案(最快的atm(但仍然很慢),并保持顺序):

server

  router.post('/ images',function(req,res,next){
var image如果(err){
console.log(err); $ b $($);
image.download b} else {
var resultImage = base64_encode(contents);
var index = req.body.index;
var returnObject = {
image:resultImage,
index :index

res.send(returnObject);
}
});
});

客户端查询

  $ scope.getDataset = function(){

fb.orderByChild('id')。startAt(_start).limitToFirst(_n).once value,function(dataSnapshot){

dataSnapshot.forEach(function(childDataSnapshot){
_start = childDataSnapshot.child(id)。val()+ 1;

var post = childDataSnapshot.val();
var image = post.image;

var imageObject = {
image:image,
index :position
;
position ++;
$ .ajax({
type:POST,
url:images,
data:imageObject ,
})。done(function(result){
post.image = result.image;
$ scope.data [result.index] = post;
$ scope。$ apply();
firstElementsLoaded = true;
});
})
});
};

客户端HTML

 < div ng-controller =ctrl> 
< div class =allcontent>
< div id =pageContentng-repeat =d in data track by $ index>< a href =details / {{d.key}}target =_ blank> < h3 class =text-left> {{d.title}}< a href =../ users / {{d.author}}>< span class =authorLegend> < I>由{{d.username}}< / i>< / span>< / a>< / h3>
< / a>
< div class =postImgIndexng-show ={{d.upvotes - d.downvotes> -50}}>
< a href =details / {{d.key}}target =_ blank>< img class =imgIndexng-src =data:image / png; base64,{{ d.image}}>< / A>
< / div>
< div class =postScore> {{d.upvotes - d.downvotes}} HP< / div>
< / div>
< / div>
< / div>


解决方案

您的解决方案很慢,因为您正在从您的云端存储并在您自己的服务器上提供服务。在下载和上传过程中会有延迟,使用base64编码的数据会造成33%的开销,另外,服务器在传输图像方面比较紧张,而不是专注于提供网站内容。



正如许多评论所指出的,最佳实践解决方案是使用图像的公共URL,例如:

  function getPublicUrl(filename){
returnhttps://storage.googleapis.com/${CLOUD_BUCKET}/${filename};



$ b $ p
$ b

通过使用公共URL,您直接从 Cloud Storage 利用Google全球服务基础架构。而且应用程序不必响应图像请求,释放其他请求的CPU周期。

如果您不希望漫游器使用上述方法抓取您的图片,Google建议您使用 robots.txt 文件来阻止访问您的图片。

Currently I was able to optimise performance quite a bit, but it is still somewhat slow :/

LATEST EDIT:

My current solution (the fastest atm (but still slow) and keeps order):

server

router.post('/images', function(req, res, next) {
    var image = bucket.file(req.body.image);
    image.download(function(err, contents) {
        if (err) {
            console.log(err);
        } else {
            var resultImage = base64_encode(contents);
            var index = req.body.index;
            var returnObject = {
                image: resultImage,
                index: index
            }
            res.send(returnObject);
        }
    });
});

client query

$scope.getDataset = function() {

                fb.orderByChild('id').startAt(_start).limitToFirst(_n).once("value", function(dataSnapshot) {

                    dataSnapshot.forEach(function(childDataSnapshot) {
                        _start = childDataSnapshot.child("id").val() + 1;

                        var post = childDataSnapshot.val();
                        var image = post.image;

                        var imageObject = {
                            image: image,
                            index: position
                        };
                        position++;
                        $.ajax({
                            type: "POST",
                            url: "images",
                            data: imageObject,
                        }).done(function(result) {
                            post.image = result.image;
                            $scope.data[result.index] = post;
                            $scope.$apply();
                            firstElementsLoaded = true; 
                        });
                    })  
                });
            };

client HTML

<div ng-controller="ctrl">
        <div class="allcontent">
            <div id="pageContent" ng-repeat="d in data track by $index"><a href="details/{{d.key}}" target="_blank"><h3 class="text-left">{{d.title}}<a href="../users/{{d.author}}"><span class="authorLegend"><i> by {{d.username}}</i></span></a></h3>
                </a>
                <div class="postImgIndex" ng-show="{{d.upvotes - d.downvotes > -50}}">
                    <a href="details/{{d.key}}" target="_blank"><img class="imgIndex" ng-src="data:image/png;base64,{{d.image}}"></a>
                </div>
                <div class="postScore">{{d.upvotes - d.downvotes}} HP</div>
            </div>
        </div>
    </div>

解决方案

Your solution is slow because you are downloading the images from your Cloud Storage and serving them on your own server. You get a delay on the download and upload, a ~33% overhead using base64-encoded data, plus your server is strained in delivering images instead of focusing on delivering your website content.

As pointed by many on the comments, the best-practice solution is to use the public URL for the images like so:

function getPublicUrl (filename) {
  return "https://storage.googleapis.com/${CLOUD_BUCKET}/${filename}";
}

By using the public URL, you are directly serving from Cloud Storage leveraging Google’s global serving infrastructure. And the application does not have to respond to requests for images, freeing up CPU cycles for other requests.

If you do not want bots to crawl your images using the above method, Google recommends using a robots.txt file to block access to your images.

这篇关于为什么我的解决方案如此之慢以及如何提高查询的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆